【问题标题】:Change swagger ui base path in spring boot在 Spring Boot 中更改 swagger ui 基本路径
【发布时间】:2018-09-20 19:23:05
【问题描述】:

在我的 Spring Boot 应用程序中,我使用 swagger 来记录文档。我需要将默认的http://localhost:8080/swagger-ui.html 路径更改为http://localhost:8080/docs/swagger-ui.html,因为我有以下与默认 swagger-ui 路径冲突的控制器。

 @RequestMapping("/{coll}")
    public List<Map> getData(@PathVariable String coll){

        ....
        return list;
 }

我搜索了很多资源(例如:https://github.com/springfox/springfox/issues/1443)并提出了很多解决方案,但对我没有任何帮助。由于这是 Swagger 中非常基本的要求,将swagger-ui.html 默认路径更改为自定义路径的最佳方法是什么?

【问题讨论】:

标签: spring spring-boot swagger swagger-ui


【解决方案1】:

您可以使用下面的代码进行更改。注意我使用的是 Apache CXF。所以如果您是 jersey,请相应地进行更改。基本上,您需要在配置中设置 basePath 和 Host。

        @Bean
        public Server rsServer() {
            JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
            endpoint.setBus(bus);
            endpoint.setAddress("/gbservice");
            endpoint.setServiceBeans(Arrays.<Object>asList(new HelloResourceImpl()));
            endpoint.setFeatures(Arrays.asList(swagger2Feature()));
            endpoint.setProvider(jsonProvider());
            return endpoint.create();
        }
        @Bean("swagger2Feature")
        public Feature swagger2Feature() {
            Swagger2Feature result = new Swagger2Feature();
            result.setTitle("Spring Boot + CXF + Swagger Example");
            result.setDescription("Spring Boot + CXF + Swagger Example description");
            result.setBasePath("/gb");
            result.setHost("http://localhost:8080/");
            result.setVersion("v1");
            result.setContact("Gaurao Burghate");
            result.setSchemes(new String[] { "http", "https" });
            result.setPrettyPrint(true);
            return result;
        }

原来我的url是http://localhost:8080/services/swagger.json,改了上面的配置后,url变成了http://localhost:8080/services/gb/swagger.json

注意您必须配置主机并且您的上下文根不应为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2016-03-01
    • 2019-02-18
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多