@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 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 "";
从源码的英文注释来看,可以加深理解。限制了这个参数可以接受的值。有三种方式去描述可接受的值:
第一种:设置一个可接受值的列表,即提供一个逗号分隔的列表。举例: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页面,会发现这个参数的选项变成了一个下拉框。