如今用Swagger来生成API文档的例子已经很是多了,今天碰到开发同事问了一个问题,帮着看了一下,主要仍是配置方法的问题,因此记录一下。若是您也碰到了一样的问题,但愿本文对您有用。ide
问题描述函数
@ApiModelProperty注解是用来给属性标注说明、默认值、是否能够为空等配置使用的,其中有一个属性allowableValues是本文要讲的重点,从属性命名上就能知道,该属性用来配置所标注字段容许的可选值。this
可是这个属性是一个String类型,咱们要如何配置可选值呢?spa
咱们能够经过源码的注释了解到一切:code
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函数定义范围等方式就能正确显示了,好比:orm
public class Filter {
@ApiModelProperty(allowableValues = "range[1,5]")
Integer order
@ApiModelProperty(allowableValues = "111, 222")
String code;
}
复制代码
再运行下程序,就能看到以下内容,设置的容许值正常显示了。 cdn