1、问题出现
偶然发现有一天,运行swagger会报java.lang.NumberFormatException: For input string: ""错误,出错的原因呢是因为 空字符串""无法转成Number。
2、分析
查阅大量资料发现是swagger版本的问题,我使用的是io.springfox:springfox-swagger2:2.9.2的版本,而该版本依赖了swagger-models的1.5.20版本(会导致报此错),深挖原因是1.5.20版本中的example只判断了是否为null,没有判断是否为空串;1.5.21版本对两者都进行了判断。

3、解决办法
可以排除springfox-swagger2中的swagger-models依赖,导入io.swagger:swagger-models的1.5.21版本即可。
在pom.xml中将swagger修改为如下:
<!--springboot 集成 swagger-->
<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>
当运行swagger时,遇到'java.lang.NumberFormatException: For input string: ""'错误,原因是空字符串不能转换。问题源于springfox-swagger2的1.5.20版本依赖。解决方案是升级到1.5.21版本或更高,确保对空字符串进行正确判断。通过修改pom.xml文件排除旧版本并引入新版本的swagger依赖可解决问题。

801

被折叠的 条评论
为什么被折叠?



