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

Sha*_*ika 5 spring swagger swagger-ui spring-boot

在我的 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;
 }
Run Code Online (Sandbox Code Playgroud)

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

Gau*_*ate 0

您可以使用下面的代码更改它。注意我使用的是 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;
        }
Run Code Online (Sandbox Code Playgroud)

所以之前我的url是http://localhost:8080/services/swagger.json,更改上面的配置后,URL变成http://localhost:8080/services/gb/swagger.json

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