Mizanur Rahman
Mizanur Rahman

Reputation: 121

How to make swagger working with jersey2 and JAX-RS on tomcat 7

I am trying to use swagger in order to document my Rest APIs. I use following link to setup with jersey2 and JAX-RS on tomcat

https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5

But I could not access either /swagger.json or /api-docs. Its responding with 404.

What am I doing wrong? Is there a workable documentation? Please help....

Upvotes: 0

Views: 1841

Answers (1)

ulrich
ulrich

Reputation: 1613

Probably you can try to better boil down, which part of the documentation is not yet working in your case.

For me, I used Jersey 2.5.1, and swagger-jersey2-jaxrs_2.10 v. 1.3.4.

I went with the solution to initialize Swagger by a bootstrap class which I included in my web.xml.

bootstrap class

public class SwaggerBootstrap extends HttpServlet {

@Override public void init(ServletConfig servletConfig) {
    try {

    ServletContext sc = servletConfig.getServletContext();

    //as of servlet api 2.5
    String ctxPath = sc.getContextPath();               
    String apiversion = "your-api-version";
    String hostname = "your-hostname";

    ConfigFactory.config().setBasePath("http://"+hostname+":8080"+ctxPath);
    ConfigFactory.config().setApiPath("http://"+hostname+":8080"+ctxPath);      
    ConfigFactory.config().setApiVersion(apiversion);       
    ConfigFactory.config().setSwaggerVersion(com.wordnik.swagger.core.SwaggerSpec.version());


    System.out.println("Swagger:");
    System.out.println("api hostname:"+hostname);
    System.out.println("context path:"+ctxPath);
    System.out.println("api-version:"+apiversion);

} catch (Exception e) {
    e.printStackTrace();
    System.out.println("Failed to configure swagger");
}
}

web.xml

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.wordnik.swagger.jersey.listing</param-value>
    </init-param>
<load-on-startup>1</load-on-startup>
  </servlet>

<servlet>
<servlet-name>JerseyJaxrsConfig</servlet-name>
   <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
   <load-on-startup>2</load-on-startup>
</servlet>

<servlet>
    <servlet-name>SwaggerBootstrap</servlet-name>
    <servlet-class>my.package.swagger.SwaggerBootstrap</servlet-class>
    <load-on-startup>2</load-on-startup>
 </servlet>

Hope that helps.

Upvotes: 0

Related Questions