解决:swagger2 Could not resolve reference because of: Could not resolve pointer: /definitions/Map«string,object» does not exist in document

问题:使用swagger时页面出现报警信息

mavaen依赖版本:2.8.0
        <!--swagger2-->
        <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>


报错信息
Errors
Resolver error at paths./queryAll.get.responses.200.schema.items.$ref
Could not resolve reference because of: Could not resolve pointer: /definitions/Map«string,object» does not exist in document

根据上面的提示是queryAll 这个方法中的Map<string,object>没法识别

queryAll方法如下:

    //查询所有记录
    @GetMapping("/queryAll")
    public List<Map<String, Object>> queryAll(){
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from jdbc_test");
        return list;
    }

返回值List<Map<String, Object>> 这种复杂的类型无法被swagger识别使用

//复杂类型需要配置:List<Map<String, Object>>
//List 是单参数的
docket.alternateTypeRules(
      AlternateTypeRules.newRule(
            typeResolver.resolve(List.class,
                 typeResolver.resolve(Map.class, String.class, Object.class)),
            typeResolver.resolve(List.class, WildcardType.class), Ordered.HIGHEST_PRECEDENCE
));

 

最后修改配置类,配置复杂类型

@Configuration //标记为配置类,启动时加载这个类
@EnableSwagger2 //此项目使用Swagger API文档
public class Swagger2Config {
    @Resource
    private TypeResolver typeResolver;

    @Bean
    public Docket api() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //指定要扫描的控制器包路径,这个路径下的类才会自动生成Swagger API文档
                //注意大小写要和实际保持一致
                .apis(RequestHandlerSelectors.basePackage("ltd.newbee.mall.controller"))
                .paths(PathSelectors.any())
                .build();
//复杂类型需要配置:List<Map<String, Object>>
        //List 是单参数的
        docket.alternateTypeRules(
                AlternateTypeRules.newRule(
                        typeResolver.resolve(List.class,
                                typeResolver.resolve(Map.class, String.class, Object.class)),
                        typeResolver.resolve(List.class, WildcardType.class), Ordered.HIGHEST_PRECEDENCE
                ));

        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-api文档") //标题
                .description("swagger文档by13") //描述
                .version("1.0") //版本
                .build();
    }
}

 

扩展:

在swagger配置中

//List<Integer> 类型就是如下表示
ResolvedType type = TypeResolver.resolve(List.class, Integer.class);

方法如果返回的是 Map<String, Map<String, List<String>>>类型,swagger同样无法识别,也需要配置

//复杂类型需要配置:Map<String, Map<String, List<String>>>
//map是双参数的
docket.alternateTypeRules(
    AlternateTypeRules.newRule(
       typeResolver.resolve(Map.class, String.class,
               typeResolver.resolve(Map.class, String.class, typeResolver.resolve(List.class, String.class))),
       typeResolver.resolve(Map.class, String.class, WildcardType.class), Ordered.HIGHEST_PRECEDENCE
));

 

 

 

 



posted on 2024-10-29 18:06  欢跳的心  阅读(42)  评论(0编辑  收藏  举报