异常解决:swagger2.9.2 报java.lang.NumberFormatException: For input string: ““...
swagger2.9.2 报java.lang.NumberFormatException: For input string: ““...
springfox-swagger 2.9.2 内置的swagger-models1.5.20 会引起Long类型格式转换异常,报错如下
java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_181] at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_181] at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_181] at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
这段报错的意思是,在将空串"",转换为Long类型时出现异常
解决方法一
对Long类型上的参数或属性上使用swagger注解时,指定example的值为数值型,比如
@ApiModelProperty(value = "公园",example="1", required = true) private Long parkId;
具体原因:
项目中很多地方都使用了swagger注解,但对Long类型添加注解时未指定example值,比如
//-------某个字段是Long类型的 这里未指定example的值,默认是空串 "" @ApiModelProperty(value = "公园", required = true) private Long parkId;
@JsonProperty("x-example") public Object getExample() { // 如果example未指定值,默认是 example="" if (example == null) { return null; } try { // 这里的 equals():只要参数不为null,并且是一个String类型,就返回true. // 判断被注解(属性,参数)的类型,进入第一个if内。 Long.valueOf("")-->抛出NumberFormatException异常被捕获 if (BaseIntegerProperty.TYPE.equals(type)) { return Long.valueOf(example); } else if (DecimalProperty.TYPE.equals(type)) { return Double.valueOf(example); } else if (BooleanProperty.TYPE.equals(type)) { if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) { return Boolean.valueOf(example); } } } catch (NumberFormatException e) { LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e); } return example; }
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)
解决方法二
将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="" ,直接返回
@JsonProperty("x-example") public Object getExample() { // example不为null 同时不为空串 这里的isEmpty():判断是否是空串 if (this.example != null && !this.example.isEmpty()) { try { if ("integer".equals(this.type)) { return Long.valueOf(this.example); } if ("number".equals(this.type)) { return Double.valueOf(this.example); } if ("boolean".equals(this.type) && ("true".equalsIgnoreCase(this.example) || "false".equalsIgnoreCase(this.defaultValue))) { return Boolean.valueOf(this.example); } } catch (NumberFormatException var2) { LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", this.defaultValue, this.type), var2); } return this.example; } else { // 如果example="" 直接返回 return this.example; } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析