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>