I am writing a JAX-RS using JAXB for marshaling my objects. I have a simple object, NameValuePair that I want to send across in PascalCase. Everything works, except it is wrapped in a camelCase wrapper, <nameValuePairs>.
This is my class:
@XmlRootElement(name = "NameValuePair")
public class NameValuePair implements Serializable {
private String name = null;
private String value = null;
public NameValuePair( String name, String value ) {
this.name = name;
this.value = value;
}
@XmlElement(name = "Name")
public String getName() {
return name;
}
@XmlElement(name = "Value")
public String getValue() {
return value;
}
}
This is how it’s used:
public Response getNameValuePairs() {
NameValuePair[] nameValuePairs;
try {
nameValuePairs = manager.getNameValuePairs();
} catch (Exception e) {
return Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.build();
}
return Response
.status(Response.Status.OK)
.entity(nameValuePairs)
.build();
}
However, the XML returned looks like this:
<nameValuePairs>
<NameValuePair>
<Name>A Name</Name>
<Value>1</Value>
</NameValuePair>
<NameValuePair>
<Name>Another Name</Name>
<Value>2</Value>
</NameValuePair>
</nameValuePairs>
How can I change <nameValuePairs> to <NameValuePairs> to match with the rest of the schema?
It seems that the array name is used for the root node of your XML. The easiest way to me, seems to wrap your entities in an List:
And return: