Search code examples
javaeclipserestjerseypayara

HTTP Status 404 Not Found For Jersey REST application with Eclipse and Payara server


I am trying to develop a Jersey server application for a booking system in Eclipse using Payara server. When I run the project I get "HTTP Status 404 - Not Found". I have checked several tutorials and StackOverflow posts but cannot find the error. Can anybody help me with this?

BookingService interface:

public interface BookingService {

    public Response addBooking(Room room);

    public Response cancelBooking(Room room);

    public Room getRoom(int roomNumber);

    public Room[] getAllAvailableRooms();
}

BookingService implementation:

@Path("/booking")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookingServiceImpl implements BookingService{

private static Map<Integer,Room> rooms = new HashMap<Integer,Room>();

@POST
@Path("/add")
public Response addBooking(Room room) {
    Response response = new Response();

    if(rooms.get(room.getNumber()).isBooked()) {
        response.setMessage("This room is not available");
        return response;
    }
    room.bookRoom();
    response.setMessage("Room successfully booked");

    return response;
}

@GET
@Path("/delete")
public Response cancelBooking(Room room) {
    Response response = new Response();
    if(!rooms.get(room.getNumber()).isBooked()) {
        response.setMessage("This room is not booked so booking cannot be cancelled");
        return response;
    }

    room.cancelBooking();
    response.setMessage("The booking for room " + room.getNumber() + "has been successfully cancelled");
    return null;
}

@GET
@Path("/getAll")
public Room[] getAllAvailableRooms() {
    Set<Integer> keys = rooms.keySet();
    Room[] roomArray = new Room[keys.size()];
    int i=0;
    for(Integer key : keys){
        if (!rooms.get(key).isBooked()) {
            roomArray[i] = rooms.get(key);
            i++;
        }
    }
    return roomArray;
}

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>se.kth.id1212</groupId>
<artifactId>ID1212BookingSystem</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ID1212BookingSystem 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>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.20</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.20</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.22.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>ID1212BookingSystem</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Edit: When I researched the problem I read that the web.xml file did not have to specify anything other than display name when using jersey 2. I don't know if that's true, but below is the web.xml file I used before reading that, but it does not work either.

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- Jersey Servlet configurations -->
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>se.kth.id1212.ID1212BookingSystem</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <!-- Jersey Servlet configurations -->
</web-app>

Edit 2, added the following images:

Project package structure

Postman 404 error


Solution

  • Even, your latest web.xml has issue, you need to go slow and compare each changes with tutorial. As per mentioned tutorials

    You should have below init-param in your web.xml

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>se.kth.id1212.ID1212BookingSystem</param-value>
    </init-param>
    

    But, instead you have written

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>se.kth.id1212.ID1212BookingSystem</param-value>
    </init-param>
    

    Also, just for side note, /add expects POST, that means it will expect request data in its body. I would suggest to use some REST client, may be POSTMAN for your testing.

    == Updated ==

    I really don't understand, why are you using below package name in your web.xml ????

    <param-value>se.kth.id1212.ID1212BookingSystem</param-value>
    

    I was definite, somewhere, you have don't blunders, the package of BookingServiceImpl should be server.service not se.kth.id1212.ID1212BookingSystem. So, in param-value, you are supposed to provide the same package name not the random one.. So, it should be

     <param-value>server.service</param-value>
    

    Also, I see, you have used Produces/Consumes annotations on class, instead it should be on each methods

     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_JSON)
     public class BookingServiceImpl implements BookingService{
    

    It's my humble request, don't mix your knowledge with available tutorial. First try to understand the implementation, by EXACTLY following any available tutorials (may be just copy/paste). Pick the simplest tutorial may be just printing Hello World