【Swagger异常】AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer

一、现象描述

最近打开Swagger 页面的时候,后台经常报错,报错信息如下:

2025-03-19 14:41:30.934  WARN 10020 --- [nio-8080-exec-4] 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_192]
	at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_192]
	at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_192]
	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_192]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_192]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_192]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_192]
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:689) [jackson-databind-2.13.4.2.jar:2.13.4.2]
二、报错原因

这是由于实体类使用@ApiModelProperty时,example属性没有赋值导致的,在AbstractSerializableParameter的getExample方法中会将数值属性的example的转换数值类返回,example的默认值是"",因此当example没有赋值时,会出现上面的异常。getExample方法,请查看源码对比

三、解决方案

关于这个错误,有很多不同的解决方法,以下是我总结的三种方法。

1.方法1:优先级法

这是一个很简单的maven配置方法,不需要exclusions进行排除,使用了maven配置的优先级方式。同路径目录下,谁先声明谁优先,把1.5.21放在上面,即可排除springfox-swagger2依赖的1.5.20版本。如下:

<dependency>
	<groupId>io.swagger</groupId>
	<artifactId>swagger-annotations</artifactId>
	<version>1.5.21</version>
</dependency>
<dependency>
	<groupId>io.swagger</groupId>
	<artifactId>swagger-models</artifactId>
	<version>1.5.21</version>
</dependency>
 
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
</dependency>
2.方法2:排除法
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<!--排除swagger2的annotations和models依赖,然后再引入1.5.21版本的annotations和models依赖-->
	<exclusions>
		<exclusion>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-annotations</artifactId>
		</exclusion>
		<exclusion>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-models</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--引入1.5.21版本的annotations和models依赖-->
<dependency>
	<groupId>io.swagger</groupId>
	<artifactId>swagger-annotations</artifactId>
	<version>1.5.21</version>
</dependency>
<dependency>
	<groupId>io.swagger</groupId>
	<artifactId>swagger-models</artifactId>
	<version>1.5.21</version>
</dependency>
3.方法3:修改@ApiModelProperty默认值

把每一个数值类型上@ApiModelProperty的example都赋值数字字符串即可。例如:


@ApiModelProperty(value = "书籍id", example = "2")
private Integer id;
 
@ApiModelProperty(value = "价格", example = "3.0")
private Double price;
@Data
public class Book {
 
    @ApiModelProperty(value = "书籍id", example = "2")
    private Integer id;
 
    @ApiModelProperty(value = "书籍名称")
    private String bookName;
 
    @ApiModelProperty(value = "价格", example = "3.0")
    private Double price;
 
    @ApiModelProperty(value = "创建人")
    private String createUser;
 
    @ApiModelProperty(value = "创建时间")
    private String createTime;