How to change basePath for Springfox Swagger 2.0

牧云@^-^@ 提交于 2020-04-07 17:37:44

问题


I'm running a service, where Swagger UI is accessible at:

http://serviceURL/swagger-ui.html

However, it is behind a proxy, such as:

http://proxyURL/serviceName

Generated URLs by Swagger UI are looking like:

http://proxyURL/

instead of the actual URL with the serviceName as suffix. As far as I get it, this means manipulating the basePath property. As per documentation:

A swagger API documentation can no longer describe operations on different base paths. In 1.2 and earlier, each resource could have had a separate basePath. In 2.0, the basePath equivalents (schemes+host+basePath) are defined for the whole specification.

@Api(basePath) is deprecated, and it doesn't say what to use and how to use it. How to make the paths generated by Swagger appear properly?

I'm using Spring Boot, Springfox Swagger and annotations.


回答1:


@Bean
public Docket newsApi(ServletContext servletContext) {
    return new Docket(DocumentationType.SWAGGER_2).pathProvider(new RelativePathProvider(servletContext) {
        @Override
        public String getApplicationBasePath() {
            return "/serviceName" + super.getApplicationBasePath();
        }
    }).host("proxyURL");
}


来源:https://stackoverflow.com/questions/36198827/how-to-change-basepath-for-springfox-swagger-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!