zoukankan      html  css  js  c++  java
  • Swagger中配置了@ApiModelProperty的allowableValues属性但不显示的问题

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

    问题描述

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

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

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

    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函数定义范围等方式就能正确显示了,比如:

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

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

  • 相关阅读:
    防抖和节流
    关于keep-alive的学习
    elementUI上传图片前判断图片的尺寸大小
    vue 判断线上环境还是本地环境
    elementUi table表格的拖拽功能
    获取文件的md5值
    element ui上传腾讯云,更新视频时长
    js音视频文件的时长
    手写一个移动端带惯性的轮播图vue组件
    这个用来总结一些常用的工具函数
  • 原文地址:https://www.cnblogs.com/didispace/p/9824789.html
Copyright © 2011-2022 走看看