报错详情:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_191]
at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_191]
at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_191]
at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412)
...
报错原因:
该异常出现于springfox-swagger2的2.9.2版本,在对@ApiModelProperty注解进行处理时,对于example属性存在要求。如使用@ApiModelProperty的字段为Integer,则需要标注为@ApiModelProperty(example = "100"),若无example标注,则会默认为空字符串 "",所以引发字符串转数字的异常。
此异常属于swagger-models的Bug,在1.5.21版本进行了修复。由于springfox-swagger2的2.9.2版本依赖的是swagger-models的1.5.20版本,所以出现该异常。
解决方法:
1.将springfox-swagger2中的swagger-models进行移除,并自行添加该依赖。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<artifactId>swagger-models</artifactId>
<groupId>io.swagger</groupId>
</exclusion>
</exclusions>
</dependency>
<!--1.5.21版本及以上均可 但建议只提升至1.5.21-->
<!--若要提高至高版本,需同时对swagger-annotations进行移除和手动添加,以防止不兼容-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
<scope>compile</scope>
</dependency>
2.将pom文件中的springfox-swagger2版本进行修改,2.9.2版本出错,降为2.8.0即可。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
本文解决springfox-swagger2中因@ApiModelProperty(example)属性处理不当导致的NumberFormatException问题,提供升级依赖和版本回退两种解决方案。
1127

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



