使用swagger2.9.2,报For input string: “”错误

什么是swagger?

Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。简单来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端分离解决方案。

使用swagger2.9.2,报java.lang.NumberFormatException:For input string: “”错误,解决方案:

springfox-swagger 2.9.2 内置的swagger-models1.5.20 会引起Long类型格式转换异常,报错如下:
使用swagger2.9.2,报For input string: “”错误_第1张图片

这段报错的意思是,在将空串"",转换为Long类型时出现异常。

具体原因:

项目中很多地方都使用了swagger注解,但对Long类型添加注解时未指定example值,比如:

 @ApiModelProperty(value = "链码id")
 private Long id;

进入AbstractSerializableParameter类查看 getExample()方法
使用swagger2.9.2,报For input string: “”错误_第2张图片

复现异常:

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


 io.springfox
 springfox-swagger2
 2.9.2
 
 
  io.swagger
 swagger-models
  


 io.swagger
 swagger-models
 1.5.22

新版对这个问题进行了修复,查看getExample()
如果example="" ,直接返回
使用swagger2.9.2,报For input string: “”错误_第3张图片

你可能感兴趣的:(java参数swagger)