如何更改 Springfox Swagger 2.0 的 basePath [英] How to change basePath for Springfox Swagger 2.0

查看:149
本文介绍了如何更改 Springfox Swagger 2.0 的 basePath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一项服务,可在以下位置访问 Swagger UI:

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

由 Swagger UI 生成的 URL 如下所示:

Generated URLs by Swagger UI are looking like:

http://proxyURL/

而不是以 serviceName 作为后缀的实际 URL.据我所知,这意味着操作 basePath 属性.根据文档:

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:

swagger API 文档无法再描述对不同的基本路径.在 1.2 及更早版本中,每个资源都可以有一个单独的 basePath.在 2.0 中,basePath 等价物(schemes+host+basePath) 是为整个规范定义的.

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) 已弃用,它没有说明使用什么以及如何使用它.如何让 Swagger 生成的路径正确显示?

@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?

我正在使用 Spring Boot、Springfox Swagger 和注释.

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

推荐答案

@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");
}

这篇关于如何更改 Springfox Swagger 2.0 的 basePath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆