【Swagger异常】AbstractSerializableParameter:Illegal DefaultValue null for parameter type integer解决方案

本文目录

一、现象描述

二、报错原因

三、解决方案

方法1:优先级法

方法2:排除法

方法3:修改@ApiModelProperty默认值

方法4:修改Swagger2的源码

四、源码对比

1、1.5.20版本源码

2、1.5.21版本源码


一、现象描述

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

AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:421):Illegal DefaultValue null for parameter type integer
java.lang.NumberFormatException: For input string: ""

[2020-04-28 11:32:15 WARN  i.s.m.p.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:421) http-nio-8080-exec-5   :] 
Illegal DefaultValue null for parameter type integer
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 io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	........
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

二、报错原因

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

三、解决方案

关于这个错误,有很多不同的解决方法,以下是我总结的四种方法,前三种已经试过了,完全没有问题,第四种方法由于要修改源码,所以不建议使用方法4。

方法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:排除法

<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:修改@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;
}

方法4:修改Swagger2的源码

修改Swagger2的源码(可能产生未知风险,不建议使用)。将源码中的if (example == null)改为if (example == null || example.isEmpty())就可以解决问题。下载并修改了源码,将其打包后覆盖了maven仓库的jar包,这样项目代码不需要任何修改就可以解决问题。

四、源码对比

以下是1.5.20和1.5.21两个版本的源码,源码路径是

io.swagger.models.parameters.AbstractSerializableParameter.getExample()

可以对比一下:

1、1.5.20版本源码

@JsonProperty("x-example")
public Object getExample() {
    if (this.example == null) {
        return null;
    } else {
	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;
    }
}

2、1.5.21版本源码

@JsonProperty("x-example")
public Object getExample() {
    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 {

        return this.example;
    }
}

 

完结!

  • 19
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No8g攻城狮

向每一个努力改变现状的你致敬!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值