Piotr Sagalara
Piotr Sagalara

Reputation: 2485

JPA, REST, unmarshalling the list of elements

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.

Upvotes: 1

Views: 608

Answers (1)

Ilya
Ilya

Reputation: 29693

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

Upvotes: 1

Related Questions