user3044240
user3044240

Reputation: 621

Resource not found in Jersey (Error 404)

I am making an AJAX call to a Jersey resource through JavaScript where class is mapped to /books and a GET method to /allBooks. This is the code for AJAX call:

function libBooks(){

    alert("Inside js");
    //The above alert is displayed

    var requestData = {
            "dataType":  "application/json",
            "type":   "GET",
            "url":    "http://localhost:8080/library/rest/books/allBooks/"
    };

    var request = $.ajax(requestData);
    alert("Ajax call made");
    //The above alert is displayed

    request.success(function(data) {

        alert("Inside done function");
        //The above alert is not displayed
        var dataReceived =data.listOfBooks;
        var numOfItems = dataReceived.length;
        alert(numOfItems);

    });

    request.fail(function(jqXHR, status, errorMessage) {
        if((errorMessage = $.trim(errorMessage)) === "") {
            alert("An unspecified error occurred.  Check the server error log for details.");
        }
        else {
            alert("An error occurred:  " + errorMessage);
        }
    });

}

In JSP I have a link like this:

<p>List of books in library  <a href="" onclick="libBooks();">Click Here</a></p>

And here is my BookResource:

@Path("/books")
public class BookResource {

    @GET
    @Path("/allBooks")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAllBooks() {

        BooksHelper bookHelper = new BooksHelper();
        ArrayList<Book> listOfBooks = bookHelper.getListOfBooks();

        ResponseBuilder responseBuilder = Response.status(Status.OK);
        responseBuilder.entity(listOfBooks);

        Response response = responseBuilder.build();
        return response;            
    }
}

Here is my 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>org.sudhanshu</groupId>
    <artifactId>library</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>library</name>

    <build>
        <finalName>library</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        -->
      <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

    </dependencies>
    <properties>
        <jersey.version>2.16</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

I am not sure how mapping in pom.xml works as I am using Maven for the first time. Here is my web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <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>org.sudhanshu.library</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

When I get to the URL: http://localhost:8080/library/rest/books/allBooks , it says resource not found 404. I think the problem might be in web.xml REST request entry point and my mapping in Resource. Also, please suggest if URL in my ajax request is correct or not. I have tried various things but none of them are working. For now I just want an alert that shows that ajax call was successful.Here is my directory structure: Any help will be appreciated. Thank You.

enter image description here

Upvotes: 3

Views: 4881

Answers (2)

FLBKernel
FLBKernel

Reputation: 860

This helped me find out what was my problem: I was having the same issue (resource not found) and I tried to establish my <param-value> tag with only the name of the package where my resource is (not nameOfMyApp.myResource). I think it could be helpful to know it as few colleagues where having also the same mistake as I was.

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 208944

Look at here

<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>org.sudhanshu.library</param-value>
</init-param>

You're specifying the package(s) to scan is org.sudhanshu.library. Looking at the image of your project structure, that package is empty. The package you should be scanning is com.resources

Second thing (unrelated to the 404, but will be the next problem) is you need a JSON provider. You can see in the generated pom.xml file they give you a hint

<!-- uncomment this to get JSON support
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
</dependency>
-->

You need to un-comment that dependency. Also I would change the dependency from jersey-media-moxy to jersey-media-json-jackson. IMO Jackson works a lot better than MOXy in many aspects.

Upvotes: 4

Related Questions