springfox 在使用 @ApiModelProperty 注释的字段时隐藏 allowEmptyValue [英] springfox hide allowEmptyValue when field annotated with @ApiModelProperty

查看:238
本文介绍了springfox 在使用 @ApiModelProperty 注释的字段时隐藏 allowEmptyValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I hide the allowEmptyValue description of response type on swagger-ui.html.

springfox 版本:2.8.0

springfox version: 2.8.0

springfox-ui 版本:2.8.0

springfox-ui version: 2.8.0

推荐答案

也许自定义的属性生成器插件可以提供帮助,请尝试将 allowEmptyValue 设置为 null.

Maybe a customized property builder plugin can help, try to set allowEmptyValue to 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);
    }
}

这篇关于springfox 在使用 @ApiModelProperty 注释的字段时隐藏 allowEmptyValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆