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

java.lang.ClassCastException while sending data in JAX-RS

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

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

这是我的 messagesDTO class:

@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 资源 class:

@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>

如果你想要 return 列表,你需要将 ArrayList 添加到 javax.ws.rs.core.GenericEntity;

将以下几行添加到您的代码中

GenericEntity<List<messagesDTO>> entity = new GenericEntity<List<messagesDTO>>(res) {}; 
    Response response = Response.ok(entity).build();


检查我们的这个答案供您参考
A message body writer for Java class java.util.ArrayList...and MIME media type text/xml was not found

你的演员表不正确,你不需要那个。

尝试以下操作之一(注意方法的 return 类型和正在 returned 的类型):

@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();
}