Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling multiple basepath in swagger

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",
......
......
}
like image 1000
noor Avatar asked Aug 20 '15 18:08

noor


1 Answers

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>
like image 169
noor Avatar answered Oct 24 '22 01:10

noor