【问题标题】:Adding base path to swagger documentaion向 swagger 文档添加基本路径
【发布时间】:2020-06-24 20:43:38
【问题描述】:

我正在尝试更改 swagger codumentation 的基本路径。目前我有

@RequestMapping(path = "/api/resourceName/v1")

和大摇大摆的配置

 return new Docket(DocumentationType.SWAGGER_2).
                select()
                .apis(RequestHandlerSelectors.basePackage("com.company"))
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .apiInfo(apiInfo());

这是给大摇大摆的基本路径"basePath": "/"

我想将基本路径添加为"basePath": "/api",所以我按照How to change basePath for Springfox Swagger 2.0 这样的差异线程添加了

return new Docket(DocumentationType.SWAGGER_2).
                select()
                .apis(RequestHandlerSelectors.basePackage("com.company"))
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .apiInfo(apiInfo())
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/api";
                 }
            });

现在基本路径更改为"basePath": "/api",并且我将路径映射更新为@RequestMapping(path = "/resourceName/v1"),因为已添加基本路径。

当我从 swagger 发送请求时,请求将发送到 /api/resourceName/v1,但服务返回 404

当我通过邮递员发送/resourceName.v1 的请求时,它可以工作。

所以 api 被注册为/resourceName/v1 并且 base 只是由 swagger 在它上面添加的,如果请求通过 swagger UI 发送将不起作用

然后我将server.servlet-path=/api添加到application.properties以在请求映射中注册basepath,现在swagger将基本路径显示为/api,无需额外配置。

但问题是现在可以在http://localhost:8080/api/swagger-ui.html 而不是http://localhost:8080/swagger-ui.html 上获得swagger 文档。因为我们在http://host/swagger-ui.html 有我们所有的其他服务文档,所以这没什么用。

有什么方法可以添加基础并仍然可以访问http://host/swagger-ui.html 的文档,并且 api 的工作与 swagger 和 postman 的预期一样

【问题讨论】:

    标签: spring-boot swagger swagger-ui swagger-2.0


    【解决方案1】:

    是的,您可以为所有 swagger 请求添加基本路径。为此,我使用了以下配置:

    @Configuration
    @EnableSwagger2
    public class SpringFoxConfig {
        @Bean
        public Docket api() {
            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
    
            docket.pathMapping("api");
    
            return docket;
        }
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2021-04-05
      相关资源
      最近更新 更多