Swagger的@ApiModelProperty和@ApiImplicitParam的allowableValues属性

@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页面,会发现这个参数的选项变成了一个下拉框。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值