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

 

至此問題解決。

 

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