【解決問題】Swagger-ui頁面報錯:Unable to infer base url. This is common when using**,Swagger與lombok衝突

一、錯誤描述

在這裏插入圖片描述

Unable to infer base url. This is common when using dynamic servlet 
registration or when the API is behind an API Gateway. 
The base url is the root of where all the swagger resources are served. 
For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. 
Please enter the location manually: 

我的操作只是將master代碼合併到個人分支上,然後跑本地代碼就出現這個錯誤,master代碼正常運行,網上的解決方式大致以下幾種

  • 1.切換swagger版本,從2.9.2到2.7.0,切換版本的同時清除瀏覽器緩存
  • 2.在啓動類加@EnableSwagger2
  • 3.攔截器過濾swagger權限

不過以上方式麼有解決我的問題,最後將master與本地分支代碼進行一一比對,因爲我修改的代碼比較少,在帥行師哥的幫助下找到原因

二、找到原因

在這裏插入圖片描述
原因:

1.lombok註解@Accessors(chain=true):作用讓setter方法返回當前對象

2.繼承BaseEntity,BaseEntity封裝了數據庫常用字段比如,id,加入getter和setter方法,setter方法返回void

3.因爲在繼承了BaseEntity之後又加入了id,此時產生衝突
@Accessors(chain=true)讓id的setter方法返回當前對象
BaseEntity讓id的setter方法返回id值

查資料說,@Accessors(chain=true)會和swagger的註解產生了衝突,導致swagger獲取不到實體類的屬性,具體原因還有待研究,大佬們可以在評論區留言哦

三、解決方法

  • 1.去掉lombok 註解@Accessors(chain = true)
  • 2.去掉自定義的private String id;

這樣就不產生衝突了,所以以後用lombok的時候要小心一點了
附上@Accessors(chain = true)詳細解讀

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