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

时间:2016-05-22 08:53:40

标签: java jax-rs jersey-2.0

我正在尝试使用Jersey 2以XML格式发送List<MessageDTO>,但获得以下异常:

  

javax.servlet.ServletException:java.lang.ClassCastException:org.glassfish.jersey.message.internal.OutboundJaxrsResponse无法强制转换为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>

2 个答案:

答案 0 :(得分:1)

如果要返回列表,则需要将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

答案 1 :(得分:1)

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

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

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