我正在搜索使用JSON和RESTful示例时发生的这个问题,但我找不到任何东西。 这是我的代码:
@Path("students")
public class StudentResource {
private StudentRepository studentRepository = new StudentRepositoryStub();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Student> getallStudentList() {
return studentRepository.findAllStudents();
}
}
这是web.xml:
<servlet>
<servlet-name>Example API</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Example API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
并且pom文件是:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>untitled67</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
最后这是学生班:
@XmlRootElement
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
学生班充满了:
public class StudentRepositoryStub implements StudentRepository {
public List<Student> findAllStudents() {
List<Student> studentList = new ArrayList<Student>();
Student student = new Student();
student.setName("jamshid");
student.setAge(22);
studentList.add(student);
student.setName("saeid");
student.setAge(26);
studentList.add(student);
return studentList;
}
}
当我使用
时MediaType.APPLICATION_JSON
而不是
MediaType.APPLICATION_XML
发生此错误:
javax.ws.rs.WebApplicationException:HTTP 500内部服务器错误 at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:267) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1035) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:947) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:938) 在com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:399) 在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:478) 在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:663) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:221) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:107) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:90) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1012) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:642) 在org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:223) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1597) 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1555) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745)
我不知道为什么!它只是作为XML工作!请帮助我tnx。
答案 0 :(得分:3)
您需要做的两件事就是
添加依赖
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.6.0</version>
</dependency>
阅读文档以获取更多详细信息:http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html