title: JAX-RS: How to automatically serialize a collection when returning a Response object?
body: <p>I have a JAXB-annotated employee class:</p>
  
  <pre class="lang-java prettyprint-override"><code>@XmlRootElement(name = "employee")
  public class Employee {
  
      private Integer id;
      private String name;
  
      ...
  
      @XmlElement(name = "id")
      public int getId() {
          return this.id;
      }
  
      ... // setters and getters for name, equals, hashCode, toString
  }
  </code></pre>
  
  <p>And a JAX-RS resource object (I'm using Jersey 1.12)</p>
  
  <pre class="lang-java prettyprint-override"><code>@GET
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Path("/")
  public List&lt;Employee&gt; findEmployees(
      @QueryParam("name") String name, 
      @QueryParam("page") String pageNumber,
      @QueryParam("pageSize") String pageSize) {
      ...
      List&lt;Employee&gt; employees = employeeService.findEmployees(...);
  
      return employees;
  }
  </code></pre>
  
  <p>This endpoint works fine. I get</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;employees&gt;
    &lt;employee&gt;
      &lt;id&gt;2&lt;/id&gt;
      &lt;name&gt;Ana&lt;/name&gt;
    &lt;/employee&gt;
  &lt;/employees&gt;
  </code></pre>
  
  <p>However, if I change the method to return a <code>Response</code> object, and put the employee list in the response body, like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@GET
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Path("/")
  public Response findEmployees(
      @QueryParam("name") String name, 
      @QueryParam("page") String pageNumber,
      @QueryParam("pageSize") String pageSize) {
      ...
      List&lt;Employee&gt; employees = employeeService.findEmployees(...);
  
      return Response.ok().entity(employees).build();
  }
  </code></pre>
  
  <p>the endpoint results in an HTTP 500 due to the following exception:</p>
  
  <blockquote>
    <p><code>javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/xml was not found</code></p>
  </blockquote>
  
  <p>In the first case, JAX-RS has obviously arranged for the proper message writer to kick in when returning a collection.  It seems somewhat inconsistent that this doesn't happen when the collection is placed in the entity body.  What approach can I take to get the automatic JAXB serialization of the list to happen when returning a response?</p>
  
  <p>I know that I can</p>
  
  <ul>
  <li>Just return the list from the resource method</li>
  <li>Create a separate <code>EmployeeList</code> class</li>
  </ul>
  
  <p>but was wondering whether there is a nice way to use the <code>Response</code> object and get the list to serialize <em>without</em> creating my own wrapper class.</p>