0
votes

I'm trying to implement Jersey 2.13 with Apache tomcat 8.0.45 and my continuously getting HTTP status 404. I feel like it is because of Jersey version mismatch with the Apache tomcat 8. I tried with different versions, so far no luck. Following is my code and please help me to sort this issue.

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>DMSAPI</groupId>
    <artifactId>DMSAPI</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.13</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.13</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>


</build>

UpdateUser.java

package com.dms.dmsapi.api;

import java.util.ArrayList;

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.QueryParam;
import javax.ws.rs.core.Response;

import org.json.JSONObject;

import com.dms.dmsapi.controller.DbCon;




@Path("/dms")
public class UpdateUser {
    @Path("/update")
    @GET
    @Produces("application/json")
    public Response convertFtoCfromInput(
            @QueryParam("callerNumber") String callerNumber,
            @QueryParam("agentID") String agentID)
            throws Exception {

        JSONObject jsonObject = new JSONObject();
        DbCon db = new DbCon();
        int resultValue = db.updateBookTaxi(callerNumber, agentID);
        System.out.println("S insert value is " + resultValue);
        if (resultValue == 1) {
            jsonObject.put("result", "success");
        } else {
            jsonObject.put("result", "error");
        }
        String result = jsonObject.toString();

        return Response.status(200).entity(result).build();
    }
}

URL - http://localhost:8080/DMSAPI/UpdateUser/dms/update?callerNumber=123&agentID=123

1
Why do you have /UpdateUser in your URL? And why do you use a GET to update something i your database? Get is for... getting.JB Nizet
Isn't that the way we call API, with class name and path? By the way I tried removing UpdateUser. But same exception is returned.dmaprasad
No, it's not. The class name is completely irrelevant. Show how you configured Jersey. And take some time to read its documentation instead of relying on incorrect assumptions.JB Nizet
Thanks. I'll check again. But with tomcat 7, this approach is working (with different versions of jersey). That is why I thought this could be because of the version mismatch.dmaprasad
Can you share your web.xml?Rishikesh Darandale

1 Answers

0
votes

Please refer following complete source code and it is working fine.

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>DMSAPI</groupId>
    <artifactId>DMSAPI</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

    </build>

    <dependencies>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-all</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.14</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
    </dependencies>
</project>

UpdateUser.java

import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.json.JSONObject;

import com.dms.dmsapi.controller.DbCon;


@Path("/")
public class UpdateUser {
    @Path("update")
    @GET
    @Produces("application/json")
    public Response convertFtoCfromInput(
            @QueryParam("callerNumber") String callerNumber,
            @QueryParam("agentID") String agentID)
            throws Exception {

        JSONObject jsonObject = new JSONObject();
        DbCon db = new DbCon();
        int resultValue = db.updateBookTaxi(callerNumber, agentID);
        System.out.println("S insert value is " + resultValue);
        if (resultValue == 1) {
            jsonObject.put("result", "success");
        } else {
            jsonObject.put("result", "error");
        }
        String result = jsonObject.toString();

        return Response.status(200).entity(result).build();
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>DMSAPI</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

URL - http://localhost:8080/DMSAPI/api/update?callerNumber=123&agentID=123

Hope someone will get help from this. Thanks :)