什么是swagger?
Swagger是一个标准和残缺的框架,用于生成、形容、调用和可视化RESTful格调的Web服务。简略来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端拆散解决方案。
应用swagger2.9.2,报java.lang.NumberFormatException:For input string: “”谬误,解决方案:
springfox-swagger 2.9.2 内置的swagger-models1.5.20 会引起Long类型格局转换异样,报错如下:
这段报错的意思是,在将空串"",转换为Long类型时出现异常。
具体起因:
我的项目中很多中央都应用了swagger注解,但对Long类型增加注解时未指定example值,比方:
@ApiModelProperty(value = "链码id") private Long id;
进入AbstractSerializableParameter类查看 getExample()办法
复现异样:
public static void main(String[] args) { long l = Long.valueOf(""); System.out.println(l);}
报错如下:
Exception in thread "main" 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) at com.lianxin.werm.api.resource.room.RoomForm.main(RoomForm.java:124)
解决办法一
对Long类型上的参数或属性上应用swagger注解时,指定example的值为数值型,比方:
@ApiModelProperty(value = "链码id",example="1")private Long id;
解决办法二(终极解决方案)
将swagger中的swagger-models1.5.20 版本升到1.5.22
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <!-- 排除自带的1.5.20版本--> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions></dependency><!-- 应用1.5.22--><dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.22</version></dependency>
新版对这个问题进行了修复,查看getExample()
如果example="" ,间接返回