我通过JavaScript将AJAX调用Jersey资源,其中类映射到/ 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调用已成功.这是我的目录结构:我们将不胜感激.谢谢.
解决方法:
看这里
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.sudhanshu.library</param-value>
</init-param>
您指定要扫描的软件包是org.sudhanshu.library.查看项目结构的图像,该包为空.您应该扫描的软件包是com.resources
第二件事(与404无关,但将是下一个问题)是您需要JSON提供程序.您可以在生成的pom.xml文件中看到它们给您的提示
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
您需要取消注释该依赖性.另外,我将依赖性从jersey-media-moxy更改为jersey-media-json-jackson. IMO Jackson在许多方面的工作要比MOXy好得多.