2

I am trying to view the list of my jersey rest service methods in API Documentation using Swagger. Went through few examples/sample given in GitHub sites. But still I am not able to list out my service methods when I try to access the context-root link. Getting 404 service not found.

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.danfoss.des</groupId>
<artifactId>SampleRestProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jersey-jaxrs</artifactId>
        <version>1.5.0</version>
    </dependency>
    <!-- <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-jaxrs_2.9.1</artifactId>
        <version>1.2.0</version>
        <scope>compile</scope>
    </dependency> -->
</dependencies>
<build>
    <finalName>SampleRestProject</finalName>
</build>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
        <servlet-name>helloworld</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.listing,com.danfoss.des</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8081/SampleRestProject/</param-value>
        </init-param> -->
        <load-on-startup>1</load-on-startup>
    </servlet>
     <servlet>
        <servlet-name>JerseyJaxrsConfig</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8081/SampleRestProject/rest</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <!-- <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list> -->

</web-app>

Service java class:

package com.danfoss.des;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.danfoss.model.Track;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Path("/helloWorld")
@Api(value="helloWorld", description="Sample hello world swagger service")
public class RESTfulHelloWorld 
{
    @GET
    @Produces("text/html")
    @Path("/startingPage")
    @ApiOperation(value="Starting of the swagger service")
    public Response getStartingPage()
    {
        String output = "Staring method is invoked";
        return Response.status(200).entity(output).build();
    }
}

My Project structure

The link am trying to access to view the list: http://localhost:8081/SampleRestProject/api-docs

Can someone please help me find out where exactly am going wrong or if I am missing out anything.

0

1 Answer 1

2

The JerseyJaxrsConfig class is part of the swagger-jersey2-jaxrs library and hence is not available when deploying the webapp, because you're using swagger-jersey-jaxrs combined with Jersey 1.9 (which is good).

Simply replace

<servlet-class>io.swagger.jaxrs.config.JerseyJaxrsConfig</servlet-class>

with

<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>

to use the correct configuration class and then try accessing http://localhost:8081/SampleRestProject/rest/swagger.json again.

Also while you're at it, consider defining Swagger's basepath as a relative path so you're able to deploy the webapp on different ports.

<init-param>
    <param-name>swagger.api.basepath</param-name>
    <param-value>/SampleRestProject/rest</param-value>
</init-param>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Mr.Luciano. Now am able to list out the service methods after replacing the servlet-class as suggested by you. Appreciate for your help.
@Tameem510 Great, I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.