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>

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM