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;
}
}
