Swagger2异常:Illegal DefaultValue null for parameter type integer java
一、异常分析:
Illegal DefaultValue null for parameter type integer和
NumberFormatException: For input string: ""
从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。
(:412) ~[swagger-models-1.5.:1.5.20]
根据上面这句报错信息,点进去:412可以看到
if((type)){
return (example);
}
就是说如果实体属性类型是Integer,就把example转为Long类型,而example默认为"",导致转换错误。
二、解决办法:
方法一:
实体类中,Integer类型的属性加@ApiModelProperty时,必须要给example参数赋值,且值必须为数字类型。
@ApiModelProperty(value = "用户ID",example = "1")
private Integer userId;
方法二:
忽略原版本的swagger-annotations和swagger-models,添加1.5.21版本的
<>2.9.2</>
<dependency>
<groupId></groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${}</version>
<exclusions>
<exclusion>
<groupId></groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId></groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${}</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
亲测,可用。