解决 swagger2出现异常:Illegal DefaultValue null for parameter type integer 问题

博客详细分析了在使用Swagger时遇到的一个问题,即默认为空字符串的Integer参数在转换时抛出NumberFormatException。解决方案是更新swagger相关依赖,排除旧版本并引入新版本的swagger-annotations和swagger-models,具体版本号为1.5.21。通过这个修复,可以避免因默认值null转换Integer类型的错误。
摘要由CSDN通过智能技术生成


一、异常分析

异常语句:

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)
	at java.lang.Long.parseLong(Long.java:601)
	at java.lang.Long.valueOf(Long.java:803)
	......

从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。

在这里插入图片描述
根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以看到

if(BaseIntegerProperty.TYPE.equals(type)){
	return Long.valueOf(example);
}

就是说如果实体属性类型是Integer,就把example转为Long类型,而example默认为"",导致转换错误。

二、解决方案

我们知道报错的是at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]

查看pom.xml我们引用的是springfox的包,找到github项目# springfox/springfox

issue查询swagger-models-1.5.20.jar,查看issue,可以发现有人已经给出了解决方案

参考大佬解决方案:https://github.com/springfox/springfox/issues/2265
1、先排除swagger-annotations 和 swagger-models 依赖
2、在添加swagger-annotations 和 swagger-models 依赖
在这里插入图片描述

代码如下:

<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
	<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>
	<version>2.9.2</version>
</dependency>
<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>

随笔记录一下解决这个问题的方法,希望能帮到有需要的同学。

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值