zoukankan      html  css  js  c++  java
  • swagger 报错:illegal defaultValue null for param type integer

    swagger(版本2.9.2) 刷新报错,错误信息如下图:

     问题原因:

    根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以看到 源码,

        @JsonProperty("x-example")
        public Object getExample() {
            if (example == null) {
                return null;
            }
            try {
                if (BaseIntegerProperty.TYPE.equals(type)) {
                    return Long.valueOf(example);
                } else if (DecimalProperty.TYPE.equals(type)) {
                    return Double.valueOf(example);
                } else if (BooleanProperty.TYPE.equals(type)) {
                    if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {
                        return Boolean.valueOf(example);
                    }
                }
            } catch (NumberFormatException e) {
                LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
            }
            return example;
        }

    Illegal DefaultValue null for parameter type integer`和`NumberFormatException: For input string: ""

    从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。

    也就是说如果实体属性类型是Integer,就把 example 转为 Long 类型,而example默认为 " " ,导致转换错误。

     解决方案:

     方法1.实体类中,Integer类型的属性加 @ApiModelProperty 时,必须要给example参数赋值,且值必须为数字类型。不推荐

    @ApiModelProperty(value = "年龄",example = "22")
    private Integer age;

     方法2.新增配置,如下,推荐

         # 默认的配置
               <dependency>
                   <groupId>io.springfox</groupId>
                   <artifactId>springfox-swagger-ui</artifactId>
                   <version>2.9.2</version>
               </dependency>
    
               <dependency>
                   <groupId>io.springfox</groupId>
                   <artifactId>springfox-swagger2</artifactId>
                   <version>2。9.2</version>
               </dependency>
        # 增加两个配置
               <dependency>
                   <groupId>io.swagger</groupId>
                   <artifactId>swagger-annotations</artifactId>
                   <version>1.5.22</version>
               </dependency>
               <dependency>
                   <groupId>io.swagger</groupId>
                   <artifactId>swagger-models</artifactId>
                   <version>1.5.22</version>
               </dependency>

    然后重启启动项目访问,问题解决。

  • 相关阅读:
    文件的权限
    正则表达式
    软硬链接的学习
    linux系统中的文件类型和扩展名
    把数组排成最小的数
    整数中1出现的次数(从1到n整数中1出现的次数)
    最小的K个数
    连续子数组的最大和
    数组中出现次数超过一半的数字
    字符串的排列
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/11898613.html
Copyright © 2011-2022 走看看