spring - 在 Spring Boot 中更改 swagger ui 基本路径

标签 spring spring-boot swagger swagger-ui

在我的 Spring Boot 应用程序中,我使用了 swagger用于文档。我需要更改默认值 http://localhost:8080/swagger-ui.html路径 http://localhost:8080/docs/swagger-ui.html因为我有以下 Controller 与默认的 swagger-ui 路径冲突。

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

        ....
        return list;
 }

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

最佳答案

您可以使用以下代码更改它。 备注 我正在使用 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;
        }

所以早些时候我的网址是 http://localhost:8080/services/swagger.json ,更改以上配置后,URL变为http://localhost:8080/services/gb/swagger.json

备注 您必须需要配置主机,而且您的上下文根不应为空。

关于spring - 在 Spring Boot 中更改 swagger ui 基本路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49766074/

相关文章:

java - 在 Spring Boot 中使用 @PropertySource 读取属性文件时出错

java - Spring Boot schema.sql - 重启时删除数据库模式

java - Spring Boot @DataJpaTest 不生成 ddl

c# - 在 Swagger 中禁用 "Try It Out"

api - 使用 Swagger 和 ServiceStack 记录响应类

java - 使用 Jersey ResourceConfig 为 Swagger 设置自定义应用程序类

spring - 如何使用 Spring 5 WebClient 等待所有请求完成?

Java Spring IOC 构造函数参数注入(inject)一个 List<Integer>

java - 在并行流上传播 Sleuth baggage

java - 如何在 Spring 按个人资料启用@Scheduled 作业?