1

I have a GET function in my REST service that returns the list of objects in XML format.

@GET
@Path("all")
@Produces({"application/xml", "application/json"})
public List<Customer> findAll() {
    return getJpaController().findCustomerEntities();
}

How can I unmarshall the list of XML to the list of objects? I would like to store all these customers from database into some List or Vector of Customers objects.

siebz0r
  • 18,867
  • 14
  • 64
  • 107
Piotr Sagalara
  • 2,247
  • 3
  • 22
  • 25
  • 1
    possible duplicate of [Using JAXB to unmarshal/marshal a List](http://stackoverflow.com/questions/1603404/using-jaxb-to-unmarshal-marshal-a-liststring) – Tomasz Nurkiewicz Aug 23 '12 at 12:30

1 Answers1

1
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Response 
{
   @XmlElement
   private List<Customer> customers = new ArrayList<Customer>();

   public Response(List<Customer> customers)
   {
      this.customers = customers;
   } 

   public getCustomers()
   {
      return customers;
   }
} 

unmarshalling

javax.xml.bind.JAXB.unmarshal(source, Response.class);  

where source is any input stream (file, stream)

@GET
@Path("all")
@Produces({"application/xml", "application/json"})
public Response findAll() {
    return new Response(getJpaController().findCustomerEntities());
}
Ilya
  • 29,135
  • 19
  • 110
  • 158