解決Swagger2返回map復雜結構不能解析的問題
今天有同事用swagger2開發(fā)時,有一方法返回Map<String,List<Object>>出現(xiàn)無法解析錯誤。
Pom.xml引入的swagger版本如下:
<!--swagger start--> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.20</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <!--swagger end-->
具體原因:
swaggerconfig沒有默認添加map的復雜結構引起的,需要手動添加。
步驟:
1. 找到swaggerconfig類,在Docket方法里添加一些mapRule即可
2. 這里設計rule比較靈活,我就按標題的格式添加,其中Model.class是自定義的業(yè)務類,換成自己的即可。
docket.alternateTypeRules(AlternateTypeRules.newMapRule(String.class, List.class)); docket.alternateTypeRules(AlternateTypeRules.newMapRule(List.class, Model.class));
具體代碼如下:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); docket.alternateTypeRules(AlternateTypeRules.newMapRule(String.class, List.class)); docket.alternateTypeRules(AlternateTypeRules.newMapRule(List.class, Model.class)); return docket; } }
Swagger使用過程中遇到的坑
1、無限請求
如果swagger頁面請求有錯誤,swagger會無限嘗試訪問,后面重啟項目的時候,控制層會無限刷新出現(xiàn)日志的內容
本地的好辦,如果項目項目部署到服務器中,可能十幾分鐘產(chǎn)生幾個G的日志文件
解決方式:最簡單的方式——關閉請求報錯的瀏覽器
2、同名問題
@Api(同名的問題) 因為swagger會根據(jù)tags 的名稱查找對象,有同名對象的時候,swagger的文檔就會出現(xiàn)問題
如果swagger的某個API下出現(xiàn)不屬于該API的請求,這個就是API的同名的問題,查找相同的API名稱替換即可
3、類上的注解“/”的問題
@ApiModel(不能使用“/”)
Errors
Hide
Resolver error at paths./v1-0/Configuration/add.post.parameters.1.schema.properties.listHotCarBrandIVO.items.$ref
Could not resolve reference because of: Could not resolve pointer: /definitions/熱門車/品牌/的IVO does not exist in document
4、使用map作為返回類型報錯,
Errors
Hide
Resolver error at definitions.Map«string,List«賣車車輛信息OVO»».additionalProperties.$ref
Could not resolve reference because of: Could not resolve pointer: /definitions/List does not exist in document
兩個解決方案:升級swagger版本號,這個是我用2.8.0報錯會報錯,網(wǎng)上有說升級版本可以解決,這個我沒有去試,
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency>
我這邊的解決方案是,將map定義在對象中,面向對象編程,而且這樣生成文檔的時候,注釋也會顯示好
5、swagger版本的問題,2.8之前的版本在 路徑/{id} +@pathVarisble 這樣的寫法
2.8之前,swagger給出的類型居然是body,需要用json的格式傳這個很奇怪,
版本更新到2.8以后,路徑后面綁定的參數(shù)就是 swagger給出的類型居然是就能是param
適當?shù)母掳姹居泻锰?/p>
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
6、沒有重現(xiàn)過的一個bug
Failed to execute 'fetch' on 'Window': Failed to parse URL from http://localhost/8765undefindFailed to parse URL from http://localhost/8765undefind
我一直重啟項目 swagger沒有重現(xiàn)問題
后來我修改請求你方法上的api注釋,重啟就可以,可能是swagger上api沖突,關鍵是這個沒有提示,好暈;如果誰找到重現(xiàn)這個問題來說一下
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于Spring Boot應用ApplicationEvent案例場景
這篇文章主要介紹了基于Spring Boot應用ApplicationEvent,利用Spring的機制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent,需要的朋友可以參考下2023-03-03SpringBoot整合MQTT并實現(xiàn)異步線程調用的問題
這篇文章主要介紹了基于SpringBoot通過注解實現(xiàn)對mqtt消息處理的異步調用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11