Swagger2异常:Illegal DefaultValue null for parameter type integer
异常分析
Illegal DefaultValue null for parameter type integer
NumberFormatException: For input string: “”
从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。
根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以看到
if(BaseIntegerProperty.TYPE.equals(type)){
return Long.valueOf(example);
}
就是说如果实体属性类型是Integer,就把example转为Long类型,而example默认为"",导致转换错误。
解决办法:两种
添加example默认值
每个使用@ApiModelProperty注解的Interger类型的参数都需要加上example的默认数值,但这种修改方式只能说相当麻烦
@ApiModelProperty(value = "num",example = "1")
private Integer num;
如果每个值都需要添加,那么会很多。
升级版本1.5.21+
1.5.21+都已经修复了这个问题了,升级
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.fox.version}</version>
<!--移除swagger-models 1.5.20 依赖,存在Swagger2异常:Illegal DefaultValue null for parameter type integer问题-->
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.2</version>
</dependency>