前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swagger中配置了@ApiModelProperty的allowableValues属性但不显示的问题

Swagger中配置了@ApiModelProperty的allowableValues属性但不显示的问题

作者头像
程序猿DD
发布2018-11-22 15:26:09
5.6K0
发布2018-11-22 15:26:09
举报
文章被收录于专栏:程序猿DD程序猿DD

现在用Swagger来生成API文档的例子已经非常多了,今天碰到开发同事问了一个问题,帮着看了一下,主要还是配置方法的问题,所以记录一下。如果你也碰到了同样的问题,希望本文对您有用。

问题描述 @ApiModelProperty注解是用来给属性标注说明、默认值、是否可以为空等配置使用的,其中有一个属性allowableValues是本文要讲的重点,从属性命名上就能知道,该属性用来配置所标注字段允许的可选值。

但是这个属性是一个String类型,我们要如何配置可选值呢?

我们可以通过源码的注释了解到一切:

代码语言:javascript
复制
public @interface ApiModelProperty {

    /**
     * Limits the acceptable values for this parameter.
     * <p>
     * There are three ways to describe the allowable values:
     * <ol>
     * <li>To set a list of values, provide a comma-separated list.
     * For example: {@code first, second, third}.</li>
     * <li>To set a range of values, start the value with "range", and surrounding by square
     * brackets include the minimum and maximum values, or round brackets for exclusive minimum and maximum values.
     * For example: {@code range[1, 5]}, {@code range(1, 5)}, {@code range[1, 5)}.</li>
     * <li>To set a minimum/maximum value, use the same format for range but use "infinity"
     * or "-infinity" as the second value. For example, {@code range[1, infinity]} means the
     * minimum allowable value of this parameter is 1.</li>
     * </ol>
     */
    String allowableValues() default "";

    ...
}

我们只需要通过,分割来定义可选值,或者用range函数定义范围等方式就能正确显示了,比如:

代码语言:javascript
复制
public class Filter {

    @ApiModelProperty(allowableValues = "range[1,5]")
    Integer order
    @ApiModelProperty(allowableValues = "111, 222")
    String code;

}

再运行下程序,就能看到如下内容,设置的允许值正常显示了。

-END-

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿DD 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档