在春季靴子改变swagger ui基本路径

时间:2018-04-11 03:33:21

标签: spring spring-boot swagger swagger-ui

在我的春季启动应用程序中,我使用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默认路径更改为自定义路径的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码进行更改。注意我正在使用Apache CXF.So如果您是球衣,请相应地进行更改。基本上,您需要在配置中设置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,在更改上述配置后,网址变为http://localhost:8080/services/gb/swagger.json

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