作者:我女神不能F哭不能输丶 | 来源:互联网 | 2022-09-28 14:20
概览
在当下几乎所有的公司都采用了前后端分离的开发模式,Swagger作为了在API在线文档工具,几乎是后端开发的必备组件,最近公司的有个项目,在项目启动和打开Swagger页面时候,后端产生了大量的异常日志,虽然不影响现在的正常启动,不过,每次看到大量的异常日志,对于有代码洁癖的我,不能忍。于是乎今天抽了个时间把以下这些问题解决了,解决后再看日志,简直不能再爽歪歪啦~ 下面对问题以及解决的办法做个记录。
问题一
异常描述:No enum constant org.springframework.web.bind.annotation.RequestMethod.Get
项目启动的时候,后台日志报如下异常:

原因
根据异常栈,能看出是springfox-swagger-common执行OperationHttpMethodReader.apply()方法是抛出来的,分析日志 RequestMethod.Get 没有对应的enum,通过检查Controller中RequestMethod,发现了问题所在:

枚举类中method方法都是大写, 而我的代码中将method写成了:

导致了在获取enum类型的时候,匹配不上抛出异常。
解决办法
将httpMethod = "Get" 给成 "GET"即可。其实原来就很好奇为什么httpMethod后添加的方法名称是大写,当时也没有深入的去研究,正好借此机会知道背后的原因,这样在开发过程中才能避免很多坑~~
问题二
异常描述:java.lang.NumberFormatException:For input string:""
项目起来后,访问swagger-ui.html页面后,接口文档可以正常展示和使用,不过后台会出现异常的日志:

原因
看日志,swagger-models这个包中的AbstractSerializableParameter.getExample()方法的时候出现异常了,此外还有一条WARN日志,Illegal DefaultValue null for parameter type integer,意思是:有个默认值是空字符串的变量转换成Integer类型时异常。
通过全局检索:Illegal DefaultValue,查看如下:

发现example只判断的null这种情况,当example如果为空字符串的话,照样往下执行,Long.valueOf(example)就是抛出异常,奔溃的心,好想把他的源码改掉呀~
解决办法
分析了下maven的包依赖:

发现swagger-models的版本为 1.5.20,果断去mvnrepository上去寻找有么有新的包修复过这个bug:

发现最新的版本 1.6.2,果断换成最新版本:
io.springfox
springfox-boot-starter
io.swagger
swagger-models
io.swagger
swagger-models
1.6.2
看下源码:

发现这个bug已经修复啦,哈哈哈哈
以上就是Spring集成Swagger常见错误及解决办法的详细内容,更多关于Spring集成Swagger常见错误的资料请关注其它相关文章!