gpt4 book ai didi

java - JSON 中的 WebApplicationException 使用

转载 作者:行者123 更新时间:2023-12-01 20:13:43 26 4
gpt4 key购买 nike

我正在搜索使用 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;
}
}

学生类(class)充满了:

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 Internal Server Error 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) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:399) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:478) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:663) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:221) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:107) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:90) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1012) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:642) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1555) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

我不知道为什么!它只是作为 XML 工作!请帮助我tnx。

最佳答案

您需要做两处更改

  1. 更改 @Produces("application/json")
  2. 路径始终应从 @Path("/students") 开始
  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

关于java - JSON 中的 WebApplicationException 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46293190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com