简体   繁体   English

泽西岛找不到资源(错误404)

[英]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. 我正在通过JavaScript对Jersey资源进行AJAX调用,其中类映射到/ books,GET方法映射到/ allBooks。 This is the code for AJAX call: 这是AJAX调用的代码:

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: 在JSP中我有这样的链接:

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

And here is my BookResource: 这是我的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 这是我的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. 我不确定pom.xml中的映射是如何工作的,因为我是第一次使用Maven。 Here is my web.xml: 这是我的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. 当我到达URL: http:// localhost:8080 / library / rest / books / allBooks时 ,它说资源找不到404.我认为问题可能出在web.xml REST请求入口点和我在Resource中的映射。 Also, please suggest if URL in my ajax request is correct or not. 另外,请建议我的ajax请求中的URL是否正确。 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. 现在我只想要一个警告,显示ajax调用成功。这是我的目录结构:任何帮助将不胜感激。 Thank You. 谢谢。

在此输入图像描述

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 . 您要指定要扫描的包是org.sudhanshu.library Looking at the image of your project structure, that package is empty. 查看项目结构的图像,该包是空的。 The package you should be scanning is com.resources 应该扫描的包是com.resources

Second thing (unrelated to the 404, but will be the next problem) is you need a JSON provider. 第二件事(与404无关,但将成为下一个问题)是你需要一个JSON提供者。 You can see in the generated pom.xml file they give you a hint 您可以在生成的pom.xml文件中看到它们提供了一个提示

<!-- 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 . 此外,我将从jersey-media-moxy改为依赖于jersey-media-json-jackson IMO Jackson works a lot better than MOXy in many aspects. IMO杰克逊在许多方面比MOXy做得更好。

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). 这有助于我找出我的问题:我遇到了同样的问题(资源未找到),我试图建立我的<param-value>标签,只包含我的资源所在的包的名称(不是nameOfMyApp.myResource) 。 I think it could be helpful to know it as few colleagues where having also the same mistake as I was. 我认为知道同样很少的同事和我一样的错误也许会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM