Resource not found in Jersey (Error 404)

user3044240

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

Paul Samsotha

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Resource not found in Jersey (Error 404)

From Dev

Simple Jersey Hello World 404 resource not found

From Dev

404 error - Resource not found in Bluemix

From Dev

Web Api 404 error The resource cannot be found

From Dev

Http 404 resource not found

From Dev

Jersey REST 404 error

From Dev

jersey webservices 404 error

From Dev

Return Custom 404 Error when resource not found in Django Rest Framework

From Dev

Failed to load resource 404 (Not Found) - file location error?

From Dev

Ajax call to JsonResult in Controller fails with 404 error, "resource not found"

From Dev

Where to throw customized 404 error in jersey when HttpServer can't find a resource

From Dev

Javascript 404 Resource Not Found NodeJS

From Dev

No Such Resource 404 error

From Dev

Jersey tomcat and eclipse: error 404

From Dev

Root resource classes found jersey rest

From Dev

Root resource classes found jersey rest

From Dev

No resource found error in android

From Dev

No resource found error in android

From Dev

No resource found error

From Dev

Error 404: Not Found Fuseki

From Dev

Codeigniter 404 Not Found Error

From Dev

Error: 404 NOT FOUND

From Dev

link not found, 404 error

From Dev

Error 404 - Not Found Wildfly

From Dev

Error with dandelion datatables - Failed to load resource: the server responded with a status of 404 (Not Found)

From Dev

I am getting 404 Resource not found error while executing Servlet Can any one rectify it?

From Dev

ASP Failed to load resource: the server responded with a status of 404 (Not Found) error in server

From Dev

HTTP Error 404. The requested resource is not found when try to open a php file

From Dev

Jersey, Tomcat: The requested resource is not available error

Related Related

HotTag

Archive