0
votes

I tried to follow the documentation to integrate swagger into an existing API. This is the tutorial : https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-1.X-Project-Setup-1.5

I added the maven dependency in pom.xml -

    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jersey-jaxrs</artifactId>
        <version>1.5.0</version>
        <exclusions>
            <exclusion>
                <artifactId>jersey-server</artifactId>
                <groupId>com.sun.jersey</groupId>
            </exclusion>
            <exclusion>
               <artifactId>jersey-multipart</artifactId>
               <groupId>com.sun.jersey.contribs</groupId>
            </exclusion>
        </exclusions>
    </dependency>

Not very sure if I should add the exclusion. Then for the package scanning, I added the following. The jersey servletContainer already was existing there. I just added initializing parameters to it.

<servlet>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>
    io.swagger.jaxrs.json;
    io.swagger.jaxrs.listing;
    jp.co.mycompany.rest.api.resource
  </param-value>
</init-param> 
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <url-pattern>/itemapi/*</url-pattern>
</servlet-mapping>

Then I added the following servlet to web.xml, which is supposed to be the last step.

<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
<init-param>
  <param-name>api.version</param-name>
  <param-value>0.0.1-SNAPSHOT</param-value>
</init-param>
<init-param>
  <param-name>swagger.api.basepath</param-name>
  <param-value>http://localhost:7001//itemapi/</param-value>
</init-param>
<load-on-startup>2</load-on-startup>

My context root is "/". So now when I go to http://localhost:7001//itemapi/swagger.json i get a 404 error.

For information this is a JAX-RS Jersey 1.9 REST API deploying on WebLogic. I researched a little and some people said using custom application works instead of the package scanning servlet. I created the class below (maybe somethings wrong here):

package test;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/itemapi")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<Class<?>>();

    classes.add(jp.co.mycompany.api.resource.EventItemResource.class);
    classes.add(jp.co.mycompany.api.resource.EventItemResourceBase.class);
    classes.add(jp.co.mycompany.api.resource.EventItemResponseJsonMessageBodyWriter.class);     
    classes.add(jp.co.mycompany.api.resource.JsonMessageBodyWriter.class);
    classes.add(jp.co.mycompany.api.resource.JsonMessageBodyWriterBase.class);
    classes.add(jp.co.mycompany.api.resource.NormalItemResourceBase.class);
    classes.add(jp.co.mycompany.api.resource.NormalItemResourceCommon.class);
    classes.add(jp.co.mycompany.api.resource.NormalItemResultJsonMessageBodyWriter.class);
    classes.add(jp.co.mycompany.api.resource.ValidatingEventItemXmlRequest.class);
    classes.add(jp.co.mycompany.api.resource.ValidatingNormalItemXmlRequest.class);
    classes.add(jp.co.mycompany.api.resource.ValidatingXmlRequest.class);
    classes.add(io.swagger.jaxrs.listing.ApiListingResource.class);
    classes.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

    return classes;
    }
}

The thing is I don't know how to make sure this class is being run. Do I have to add a snippet in web.xml. The class is saved in a separate package called 'test'.

First of all why was I getting the 404 error? Did I miss something? Secondly if that is an issue how do I save and run this custom application class??

Thanks in advance for all the help!

1
HTTP 404 implies a NOT FOUND error implying URL mapping to your servlet wasn't found - thebishal
@BishalThapa exactly but why. The guide says that the initialization servlet does not have a mapping because it is only used to initialize swagger. - Rahul Dravid

1 Answers

0
votes

For anyone interested, I got it.

I did not include an important package in the package scanning servlet in web.xml therefore the api wasn't even running properly.

I reverted all my changes and decided to run the api. Through the logs I saw which packages were being scanned, since the servletcontainer servlet was blank. I then edited it again just as before, but this time keeping in mind the packages that were scanned last time.

The same link works now. And there is no need for an application subclass.