一、异常分析
异常语句:
i.s.m.p.AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:601)
at java.lang.Long.valueOf(Long.java:803)
......
从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。
根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以看到
if(BaseIntegerProperty.TYPE.equals(type)){
return Long.valueOf(example);
}
就是说如果实体属性类型是Integer,就把example转为Long类型,而example默认为"",导致转换错误。
二、解决方案
我们知道报错的是at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
查看pom.xml我们引用的是springfox的包,找到github项目# springfox/springfox
issue查询swagger-models-1.5.20.jar,查看issue,可以发现有人已经给出了解决方案
参考大佬解决方案:https://github.com/springfox/springfox/issues/2265
1、先排除swagger-annotations 和 swagger-models 依赖
2、在添加swagger-annotations 和 swagger-models 依赖
代码如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
随笔记录一下解决这个问题的方法,希望能帮到有需要的同学。