java.lang.ClassCastException while sending data in JAX-RS
<p>I am trying to send <code>List&lt;MessageDTO&gt;</code> in the form of XML using Jersey 2, but getting the following exception:</p>

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

<p>Here is my <code>messagesDTO</code> class:</p>

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

<p>And my JAX-RS resource class:</p>

<pre><code>@Path("/messages1")
public class MessageResources {

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public List&lt;messagesDTO&gt; getMessage() {

        MessageServices obj = new MessageServices();
        List&lt;messagesDTO&gt; res = obj.getAllMessages();
        return (List&lt;messagesDTO&gt;) Response.ok().entity(res).build();
    }
}
</code></pre>

<p>My <code>web.xml</code> file:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!-- 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 --&gt;
&lt;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"&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
            &lt;param-value&gt;com.messages.MessaseApp&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/webapi/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;
</code></pre>

<p>And my <code>pom.xml</code> file:</p>

<pre><code>&lt;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"&gt;

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.messages&lt;/groupId&gt;
    &lt;artifactId&gt;MessaseApp&lt;/artifactId&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;

    &lt;name&gt;MessaseApp&lt;/name&gt;

    &lt;build&gt;
        &lt;finalName&gt;MessaseApp&lt;/finalName&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.5.1&lt;/version&gt;
                &lt;inherited&gt;true&lt;/inherited&gt;
                &lt;configuration&gt;
                    &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
                    &lt;source&gt;1.7&lt;/source&gt;
                    &lt;target&gt;1.7&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
                &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
                &lt;version&gt;${jersey.version}&lt;/version&gt;
                &lt;type&gt;pom&lt;/type&gt;
                &lt;scope&gt;import&lt;/scope&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-container-servlet-core&lt;/artifactId&gt;
            &lt;!-- use the following artifactId if you don't need servlet 2.x compatibility --&gt;
            &lt;!-- artifactId&gt;jersey-container-servlet&lt;/artifactId --&gt;
        &lt;/dependency&gt;
        &lt;!-- uncomment this to get JSON support &lt;dependency&gt; &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-media-moxy&lt;/artifactId&gt; &lt;/dependency&gt; --&gt;
    &lt;/dependencies&gt;
    &lt;properties&gt;
        &lt;jersey.version&gt;2.22.2&lt;/jersey.version&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
&lt;/project&gt;
</code></pre>