解決Swagger2返回map復雜結(jié)構(gòu)不能解析的問題
今天有同事用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的復雜結(jié)構(gòu)引起的,需要手動添加。
步驟:
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)日志的內(nèi)容
本地的好辦,如果項目項目部署到服務器中,可能十幾分鐘產(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定義在對象中,面向?qū)ο缶幊蹋疫@樣生成文檔的時候,注釋也會顯示好

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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Maven的配置文件pom.xml詳解(含常用plugin)
pom.xml是Maven項目的核心配置文件,它是 項目對象模型 - Project Object Model(POM)的縮寫,本文我們將全面解析pom.xml,了解其結(jié)構(gòu)和屬性,以及如何使用它來管理項目,感興趣的朋友跟隨小編一起看看吧2024-08-08
AsyncHttpClient的ConnectionSemaphore方法源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient的ConnectionSemaphore方法源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
MyBatis批量插入大量數(shù)據(jù)(1w以上)
MyBatis進行批量插入數(shù)時,一次性插入超過一千條的時候MyBatis開始報錯,本文主要介紹了MyBatis批量插入大量數(shù)據(jù)的解決方法,感興趣的可以了解一下2022-01-01

