解决Swagger2返回map复杂结构不能解析的问题

2025-05-29 0 68

今天有同事用swagger2开发时,有一方法返回Map<String,List<Object>>出现无法解析错误。

Pom.xml引入的swagger版本如下:

  1. <!–swagger start–>
  2. <dependency>
  3. <groupId>io.swagger</groupId>
  4. <artifactId>swagger-annotations</artifactId>
  5. <version>1.5.20</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger2</artifactId>
  10. <version>2.6.1</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>io.springfox</groupId>
  14. <artifactId>springfox-swagger-ui</artifactId>
  15. <version>2.6.1</version>
  16. </dependency>
  17. <!–swagger end–>

具体原因:

swaggerconfig没有默认添加map的复杂结构引起的,需要手动添加。

步骤:

1. 找到swaggerconfig类,在Docket方法里添加一些mapRule即可

2. 这里设计rule比较灵活,我就按标题的格式添加,其中Model.class是自定义的业务类,换成自己的即可。

  1. docket.alternateTypeRules(AlternateTypeRules.newMapRule(String.class, List.class));
  2. docket.alternateTypeRules(AlternateTypeRules.newMapRule(List.class, Model.class));

具体代码如下:

  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfig {
  4. @Bean
  5. public Docket createRestApi() {
  6. Docket docket = new Docket(DocumentationType.SWAGGER_2)
  7. .apiInfo(apiInfo())
  8. .select()
  9. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  10. .paths(PathSelectors.any())
  11. .build();
  12. docket.alternateTypeRules(AlternateTypeRules.newMapRule(String.class, List.class));
  13. docket.alternateTypeRules(AlternateTypeRules.newMapRule(List.class, Model.class));
  14. return docket;
  15. }
  16. }

Swagger使用过程中遇到的坑

1、无限请求

如果swagger页面请求有错误,swagger会无限尝试访问,后面重启项目的时候,控制层会无限刷新出现日志的内容

本地的好办,如果项目项目部署到服务器中,可能十几分钟产生几个G的日志文件

解决方式:最简单的方式——关闭请求报错的浏览器

2、同名问题

@Api(同名的问题) 因为swagger会根据tags 的名称查找对象,有同名对象的时候,swagger的文档就会出现问题

如果swagger的某个API下出现不属于该API的请求,这个就是API的同名的问题,查找相同的API名称替换即可

3、类上的注解“/”的问题

@ApiModel(不能使用“/”)

解决Swagger2返回map复杂结构不能解析的问题

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报错会报错,网上有说升级版本可以解决,这个我没有去试,

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.8.0</version>
  5. </dependency>

我这边的解决方案是,将map定义在对象中,面向对象编程,而且这样生成文档的时候,注释也会显示好

解决Swagger2返回map复杂结构不能解析的问题

5、swagger版本的问题,2.8之前的版本在 路径/{id} +@pathVarisble 这样的写法

2.8之前,swagger给出的类型居然是body,需要用json的格式传这个很奇怪,

版本更新到2.8以后,路径后面绑定的参数就是 swagger给出的类型居然是就能是param

适当的更新版本有好处

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.8.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.8.0</version>
  10. </dependency>

6、没有重现过的一个bug

Failed to execute 'fetch' on 'Window': Failed to parse URL from http://localhost/8765undefindFailed to parse URL from http://localhost/8765undefind

我一直重启项目 swagger没有重现问题

后来我修改请求你方法上的api注释,重启就可以,可能是swagger上api冲突,关键是这个没有提示,好晕;如果谁找到重现这个问题来说一下

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/washingtin/article/details/102681667

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 解决Swagger2返回map复杂结构不能解析的问题 https://www.kuaiidc.com/106525.html

相关文章

发表评论
暂无评论