首页 > 编程语言 > 解决Swagger2返回map复杂结构不能解析的问题
2021
10-12

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

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

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是自定义的业务类,换成自己的即可。

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会无限尝试访问,后面重启项目的时候,控制层会无限刷新出现日志的内容

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

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

2、同名问题

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

如果swagger的某个API下出现不属于该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报错会报错,网上有说升级版本可以解决,这个我没有去试,

<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以后,路径后面绑定的参数就是 swagger给出的类型居然是就能是param

适当的更新版本有好处

<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、没有重现过的一个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冲突,关键是这个没有提示,好晕;如果谁找到重现这个问题来说一下

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧