Swagger的@ApiModelProperty和@ApiImplicitParam的a。。
@ApiModelProperty和@ApiImplicitParam中都有⼀个allowableValues属性,这个属性见名知意,就是使⽤注释地⽅所允许的值。
跟进这两个属性的源码可以看到,allowableValues属性上⾯的源码注释是相同。如下
/**
* 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 squareparam name
* 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 "";
从源码的英⽂注释来看,可以加深理解。限制了这个参数可以接受的值。有三种⽅式去描述可接受的值:
第⼀种:设置⼀个可接受值的列表,即提供⼀个逗号分隔的列表。举例:first, second, third;
第⼆种:设置以range开头的字符,提供开闭区间的功能。举例:range[1,5], range[1,5);
第三种:设置最⼤值和最⼩值,使⽤infinity,举例:range[1, infinity];即参数⼤于等于1
第⼀种⽅式适合参数为String的情况,⽐如season这个变量表⽰季度,传参只能是 spring, summer ,autumn,winter
@ApiImplicitParam(name = "season", value = "季度", allowableValues = "spring, summer, autumn, winter", required = true)
然后,打开swagger页⾯,会发现这个参数的选项变成了⼀个下拉框。