当使用@ApiModelProperty注释字段时,springfox隐藏allowEmptyValue

问题描述 投票:2回答:1

如何在swagger-ui.html上隐藏响应类型的allowEmptyValue描述。

springfox版本:2.8.0

springfox-ui版本:2.8.0

screenshot from swagger html

swagger-ui springfox
1个回答
2
投票

也许定制的属性构建器插件可以帮助,尝试将allowEmptyValue设置为null

import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiModelProperty;
import springfox.documentation.builders.ModelPropertyBuilder;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

@Component
public class CustomizedModelPropertyBuilderPlugin implements ModelPropertyBuilderPlugin {

    @Override
    public boolean supports(final DocumentationType arg0) {
        return true;
    }

    @Override
    public void apply(final ModelPropertyContext context) {
        final ModelPropertyBuilder builder = context.getBuilder();

        final Optional<BeanPropertyDefinition> beanPropDef = context.getBeanPropertyDefinition();

        if (!beanPropDef.isPresent()) {
            return;
        }

        final BeanPropertyDefinition beanDef = beanPropDef.get();
        final AnnotatedMethod method = beanDef.getGetter();
        if (method == null) {
            return;
        }

        final ApiModelProperty apiModelProperty = method.getAnnotation(ApiModelProperty.class);
        if (apiModelProperty == null) {
            return;
        }

        builder.allowEmptyValue(null);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.