RESTEasy service to create xml and returned array / list of JAXB objects giving an empty collection

I am testing RESTEasy web services. I wrote a simple service to return a list of JAXB client objects and expect the returned xml to collect Customer tags in the Collection tag. But I get <Collection/> , means an empty collection.

My code is:

Customer service

 @Path("/customers") public class CustomerService { List<Customer> customersList = new ArrayList<Customer>(); public CustomerService() { customersList.add(new Customer(.....)); //Creating Customers using parametarized Cunstructor customersList.add(new Customer(.....)); customersList.add(new Customer(.....)); customersList.add(new Customer(.....)); customersList.add(new Customer(.....)); } @GET @Produces("application/xml") @Path("/xml/list") public List<Customer> getAllCustomersList(){ return customersList; //nonEmpty list of Customers } } 

Client (JAXB Object)

 @XmlRootElement public class Customer { private String customerId; private String name; private String address; private int age; public Customer() { } public Customer(String customerId, String name, String address, int age) { super(); this.customerId = customerId; this.name = name; this.address = address; this.age = age; } @XmlAttribute public String getCustomerId() { return customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @XmlElement public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 

I can get one Client with this Service, which works very well:

 @GET @Produces("application/xml") @Path("/xml/{id}") public Customer getCustomer(@PathParam("id") String customerId){ for(Customer customer: customersList){ if(customerId.equals(customer.getCustomerId())) return customer; } return null; } 

I made a singleton service, so the list is not empty, that's for sure. Even I debugged the code to confirm that the list is not empty. I also tried with an array, and the same thing happens.

Here is what I read: http://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Built_in_JAXB_providers.html#JAXB_Collections

I use

  • WebSphere Application Server 8.0
  • RESTEasy 2.2.3GA
+4
source share
1 answer

I was able to serialize with your POJO client, making the following changes:

  • Moved @XmlElement annotations to fields
  • Added @XmlAccessorType(XmlAccessType.FIELD) annotation @XmlAccessorType(XmlAccessType.FIELD) to tell JAXB to field-bind and ignore getters / setters.

Method:

 @GET @Path("/customers") @Produces(MediaType.APPLICATION_XML) public List<Customer> getCustomer() { List<Customer> customers = new ArrayList<Customer>(); customers.add(new Customer("1", "customer1", "some address 1", 20)); customers.add(new Customer("2", "customer2", "some address 2", 45)); customers.add(new Customer("3", "customer3", "some address 3", 36)); return customers; } 

POJO Client:

 @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class Customer { @XmlAttribute private String customerId; @XmlElement private String name; @XmlElement private String address; @XmlElement private int age; public Customer() { } public Customer(String customerId, String name, String address, int age) { this.customerId = customerId; this.name = name; this.address = address; this.age = age; } public String getCustomerId() { return customerId; } public void setCustomerId(String customerId) { this.customerId = customerId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 

XML Output:

 <?xml version="1.0" encoding="UTF-8"?> <collection> <customer customerId="1"> <name>customer1</name> <address>some address 1</address> <age>20</age> </customer> <customer customerId="2"> <name>customer2</name> <address>some address 2</address> <age>45</age> </customer> <customer customerId="3"> <name>customer3</name> <address>some address 3</address> <age>36</age> </customer> </collection> 

What if you want to change the name of the wrapper element?

If you don’t like the xml list returned in the collection element and you want to change the name, you can add the @Wrapped annotation to your resource method as follows:

 @GET @Path("/customers") @Produces(MediaType.APPLICATION_XML) @Wrapped(element = "customers") public List<Customer> getCustomer() { List<Customer> customers = new ArrayList<Customer>(); customers.add(new Customer("1", "customer1", "some address 1", 20)); customers.add(new Customer("2", "customer2", "some address 2", 45)); customers.add(new Customer("3", "customer3", "some address 3", 36)); return customers; } 

This wraps the customer list in the customers element instead of collection .

 <?xml version="1.0" encoding="UTF-8"?> <customers> <customer customerId="1"> <name>customer1</name> <address>some address 1</address> <age>20</age> </customer> <customer customerId="2"> <name>customer2</name> <address>some address 2</address> <age>45</age> </customer> <customer customerId="3"> <name>customer3</name> <address>some address 3</address> <age>36</age> </customer> </customers> 
+4
source

Source: https://habr.com/ru/post/1491112/


All Articles