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

时间:2018-01-22 10:18:53

标签: swagger-ui springfox

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

springfox版本:2.8.0

springfox-ui版本:2.8.0

screenshot from swagger html

1 个答案:

答案 0 :(得分: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);
    }
}