首页 文章

在swagger中处理多个basepath

提问于
浏览
10

我使用swagger-ui为我们的客户提供REST API的好文档 . 在内部,我们有两个不同的环境jenkin构建项目 . 例如 . swagger.json可在以下两种环境中访问: http://www.myhost.com/xyz/rest/swagger.json https://www.myhost2.com/rest/swagger.json

文档可用: http://www.myhost.com/xyz/dist/index.html https://www.myhost2.com/dist/index.html

web.xml中的swagger api basepath是:

<init-param>       
     <param-name>swagger.api.basepath</param-name>
     <param-value>/rest</param-value>
</init-param>

问题:我正在尝试在文档页面上使用"Try it out"功能 . 两台主机的相应请求URL如下: http://www.myhost.com/rest/getAUser https://www.myhost2.com/rest/getAUser

它适用于host2,因为它正在访问正确的URL . 但它应该为host1命中 http://www.myhost.com/xyz/rest/getAUser 但是它正在击中网址 http://www.myhost.com/rest/getAUser .

有没有办法可以为不同的URL指定多个basepath .

我的swagger-ui html看起来像这样 .

$(function () {
var href = window.location.href;
var url = href.substring(0, href.lastIndexOf("/dist"));
console.log(url);
// Pre load translate...
if(window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
window.swaggerUi = new SwaggerUi({
url: url + "/rest/swagger.json",
dom_id: "swagger-ui-container",
......
......
}

1 回答

  • 6

    我能够通过使用BeanConfig配置swagger而不是在web.xml中使用Servlet来解决此问题

    BeanConfig类:

    public class SwaggerBootstrap extends DefaultJaxrsConfig {
    
        /**
         *
         */
        private static final long serialVersionUID = myAutoGeneratedID;
    
        @Override
        public void init(ServletConfig config) throws ServletException {
    
            super.init(config);
            //contextPath will be null for host2 and /xyz for host1.
            String contextPath = config.getServletContext().getContextPath();
    
            BeanConfig beanConfig = new BeanConfig();
            beanConfig.setVersion("1.0.0");
            beanConfig.setTitle("My API Documentation");
            beanConfig.setSchemes(new String[] {
                    "http", "https"
            });
            beanConfig
            .setResourcePackage("com.example.my.rest.api.package");
    
            beanConfig.setBasePath(contextPath + "/rest");
            beanConfig.setScan(true);
        }
    }
    

    并在web.xml中:

    <servlet>
            <servlet-name>SwaggerBootstrap</servlet-name>
            <servlet-class>my.package.to.SwaggerBootstrap</servlet-class>
            <init-param>
                <!-- This make sure that all resources are scanned whether or not they use Swagger Annotations. 
                https://github.com/swagger-api/swagger-samples/tree/master/java/java-jaxrs-no-annotations -->
                <param-name>scan.all.resources</param-name>
                <param-value>true</param-value>
            </init-param>
            <load-on-startup>2</load-on-startup>
        </servlet>
    

    我改变了我的pom.xml以开始使用最新稳定版本的swagger-jersey2-jaxrs:

    <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-jersey2-jaxrs</artifactId>
                <version>1.5.3</version>
            </dependency>
    

相关问题