【发布时间】:2015-05-22 19:14:26
【问题描述】:
我正在通过 JavaScript 对 Jersey 资源进行 AJAX 调用,其中类映射到 /books,GET 方法映射到 /allBooks。这是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);
}
});
}
在 JSP 中我有一个这样的链接:
<p>List of books in library <a href="" onclick="libBooks();">Click Here</a></p>
这是我的 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;
}
}
这是我的 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>
我不确定 pom.xml 中的映射是如何工作的,因为我是第一次使用 Maven。这是我的 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>
当我访问 URL:http://localhost:8080/library/rest/books/allBooks 时,它显示资源未找到 404。我认为问题可能出在 web.xml REST 请求入口点和我在 Resource 中的映射。另外,请建议我的ajax请求中的URL是否正确。我尝试了各种方法,但没有一个有效。现在我只想要一个显示 ajax 调用成功的警报。这是我的目录结构:任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: java ajax rest maven jersey