现象:在访问swagger首页http://localhost:8080/swagger-ui.html时,后台出现如下异常信息
2020-04-05 17:21:31.928 WARN 4632 --- [nio-8080-exec-6] i.s.m.p.AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_172]
at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_172]
at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_172]
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_172]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_172]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_172]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_172]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688) [jackson-databind-2.9.8.jar:2.9.8]
实体类定义如下
@ApiModel(value="BookInfo对象", description="图书表")
public class BookInfo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "图书编号")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
}
从异常信息看是从有个默认值是空字符串的变量转换成Integer类型时异常,在getExample(AbstractSerializableParameter.java:412处
代码意思是如果实体属性类型是Integer,就把example转为Long类型,但是example默认为"",所以导致转换错误。
解决方法:实体类中,Integer类型的属性加@ApiModelProperty时,必须要给example参数赋值,且值必须为数字类型。如下
@ApiModel(value="BookInfo对象", description="图书表")
public class BookInfo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "图书编号", example = "0")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
}