I have a relatively simple prototype using the REST features of CXF, and
using JAXB out of the box to marshal a response to XML and JSON. This
is working fine.
I'm now trying to experiment with simple changes to the structure of the
marshaled XML, using "@Xml..." annotations. I assume that's what those
are for. I tried asking about this on the Metro JAXB web forum, but I
got no response. Is there another good forum for asking questions about
this?
Initially, my code looks like this:
@XmlRootElement(name = "Item")
public class Item {
private String id;
private String title;
private String description;
private List<String> features = new ArrayList<String>();
// ... getters/setters
public void addFeature(String feature) { getFeatures().add(feature); }
}
Another class:
@GET
@Path("/item/{id}")
@Produces({"application/xml", "application/json"})
public Item getItem(@PathParam("id") String id) {
Item item = new Item();
item.setId(id);
item.setTitle("abc");
item.setDescription("def");
item.addFeature("123");
item.addFeature("456");
return item;
}
When I send a request to ".../item/1", I get back:
<Item>
<description>def</description>
<features>123</features>
<features>456</features>
<id>1</id>
<title>abc</title>
</Item>
I'd really prefer to get:
<Item>
<description>def</description>
<features>
<feature>123</feature>
<feature>456</feature>
</features>
<id>1</id>
<title>abc</title>
</Item>
I've played around with several of the "@Xml" annotations on the
"features" field, but they just give me exceptions. For instance, I
just tried a simple case of adding '@XMLElement("feature")' just before
that field, and it gave me this exception:
WARNING: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1
counts of IllegalAnnotationExceptions
Class has two properties of the same name "features"
this problem is related to the following location:
at public java.util.List
com.att.ecom.catalog.Item.getFeatures()
at com.att.ecom.catalog.Item
this problem is related to the following location:
at private java.util.List
com.att.ecom.catalog.Item.features
at com.att.ecom.catalog.Item