Handling multiple basepath in swagger

半腔热情 提交于 2020-02-21 10:16:06

问题


I am using swagger-ui to provide nice documentation for REST APIs to our clients. Internally we have two different environments jenkin builds the project to. E.g. swagger.json is accessible on both environment as: http://www.myhost.com/xyz/rest/swagger.json https://www.myhost2.com/rest/swagger.json

Documentation is available as: http://www.myhost.com/xyz/dist/index.html https://www.myhost2.com/dist/index.html

swagger api basepath in web.xml is:

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

ISSUE: I am trying to use "Try it out" feature on documentation page. The respective request url for both hosts are as follows: http://www.myhost.com/rest/getAUser https://www.myhost2.com/rest/getAUser

It works for host2 as it is hitting the correct url. However it should have hit http://www.myhost.com/xyz/rest/getAUser for host1 but it is hitting the url http://www.myhost.com/rest/getAUser.

Is there a way I can specify multiple basepath for different urls.

My swagger-ui html looks something like this.

$(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:


I was able to resolve this issue by configuring swagger using BeanConfig instead of using Servlet in web.xml

BeanConfig class:

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

and in 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>

And I changed my pom.xml to start using latest stable version of swagger-jersey2-jaxrs:

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


来源:https://stackoverflow.com/questions/32125942/handling-multiple-basepath-in-swagger

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