解决swagger2中@ApiImplicitParam注解中dataType = "integer"属性在2.9.2版本中不能正常传参测试问题
问题描述
正常在SpringBoot整合swagger后,在controller中方法上添加响应注解。在添加@ApiImplicitParam注解时,将dataType 参数设置为:Integer后,进行接口文档测试,发现输入相应参数后,不能正常执行测试。
swagger2依赖(2.9.2):
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
接口如下:
测试:
而我在将swagger相关依赖版本降低到2.7.0后,不存在上述问题。
解决办法
在不更换swagger2依赖版本的情况下,将dataType 参数设置为:Int即可解决。
此时虽然可以输入参数进行正常测试,但后台控制台会报错。报错如下:
AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer
swagger测试正常
同时后台抛出异常:
上面Swagger异常解决办法
参考一下2篇博客:
https://blog.csdn.net/boyin333/article/details/87980108
https://blog.csdn.net/weixin_38229356/article/details/83353347
通常有两种解决办法:
- 修改源码,参考:https://blog.csdn.net/weixin_38229356/article/details/83353347
- 使用pom的 exclusions方式,参考:https://blog.csdn.net/boyin333/article/details/87980108
此处采用比较简单的第二种方式:
pom依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--解决Swagger异常:AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer-->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
注意:排除swagger2本身引用的swagger-models-1.5.20.jar【这版本有bug】,引入更高的版本即可解决问题。
运行结果: