gpt4 book ai didi

在 JAX-RS 中发送数据时出现 java.lang.ClassCastException

转载 作者:行者123 更新时间:2023-12-02 03:36:44 28 4
gpt4 key购买 nike

我正在尝试发送List<MessageDTO>使用 Jersey 2 以 XML 的形式,但出现以下异常:

javax.servlet.ServletException: java.lang.ClassCastException: org.glassfish.jersey.message.internal.OutboundJaxrsResponse cannot be cast to java.util.List

这是我的messagesDTO类:

@XmlRootElement
public class messagesDTO implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@XmlAttribute
Long id;

@XmlElement
String message;

@XmlElement
String author;

@XmlElement
Date date;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getMessage() {
return message;
}

public messagesDTO(Long id, String message, String author, Date date) {
// super();
this.id = id;
this.message = message;
this.author = author;
this.date = new Date();
}

public void setMessage(String message) {
this.message = message;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public messagesDTO() {
super();
}
}

还有我的 JAX-RS 资源类:

@Path("/messages1")
public class MessageResources {

@GET
@Produces(MediaType.APPLICATION_XML)
public List<messagesDTO> getMessage() {

MessageServices obj = new MessageServices();
List<messagesDTO> res = obj.getAllMessages();
return (List<messagesDTO>) Response.ok().entity(res).build();
}
}

我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>com.messages.MessaseApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

还有我的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>com.messages</groupId>
<artifactId>MessaseApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

<name>MessaseApp</name>

<build>
<finalName>MessaseApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<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> -->
</dependencies>
<properties>
<jersey.version>2.22.2</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

最佳答案

您的 Actor 阵容不正确,您不需要它。

尝试以下方法之一(注意方法的返回类型和返回的类型):

@GET
@Produces(MediaType.APPLICATION_XML)
public List<messagesDTO> getMessage() {
MessageServices obj = new MessageServices();
List<messagesDTO> res = obj.getAllMessages();
return res;
}
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getMessage() {
MessageServices obj = new MessageServices();
List<messagesDTO> res = obj.getAllMessages();
return Response.ok().entity(res).build();
}

关于在 JAX-RS 中发送数据时出现 java.lang.ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372334/

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