knife4j/swagger救援第一现场

1、前方来报,测试环境springboot项目无法启动,现场如下:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[ERROR] 2024-03-20 12:54:42,718 --main-- [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter]

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in springfox.documentation.swagger.schema.ApiModelPropertyPropertyBuilder required a bean of type 'springfox.documentation.spring.web.DescriptionResolver' that could not be found.


Action:

Consider defining a bean of type 'springfox.documentation.spring.web.DescriptionResolver' in your configuration.

 

2、现场痕迹很明显,类ApiModelPropertyPropertyBuilder中需要一个bean DescriptionResolver,但是无法找到,故直接走入现场,如下图所示:

3、根据经验得知是bean DescriptionResolver无法被正常注入导致的,很大情况可能是因为jar冲突导致,寻找蛛丝马迹,如下图所示:

4、定位到原因是项目中引用其他项目中的jar,结果导致了项目中有两个不同版本的knife4j-spring-boot-starter(一个2.0.2,一个2.0.7),简单粗暴,把版本都调整为2.0.2。原本想着到这里问题都已经解决了。下意识的看了下前端swagger接口是否正常,结果发现swagger接口展示有问题:

页面提示:Knife4j文档请求异常
页面console控台提示:Unexpected token ' in JSON

5、谷歌浏览器前端debug一顿调试,定位问题是因为后端返回的数据结构有问题,根本原因因为版本2.0.2较低,无法解析ApiModelProperty中的example属性为json字符串,如下图所示:

属性使用ApiModelProperty注解后,在给与example类型的时候里面写了一个json字符串

@ApiModelProperty(name="xx",example="{'xx':'dd'}")
private String test;

就是类似以上这种情况,导致springfox-swagger提供的接口/v2/api-docs返回的json非法,使用前端的JSON.parse()函数无法转换为JSON对象

6、处理方案:

a、knife4j统一使用版本2.0.2去掉example

b、knife4j统一使用版本2.0.7或其他版本,可以支持example

 

至此问题解决。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章