Reputation: 91610
How does one map fields in JAXB annotations to elements and attributes? I'm having a bit of trouble trying to custom-tailor my model in JAXB. Here's my current model:
Info.java:
package com.rest.model;
public class Info {
private String a;
private String b;
private String c;
private String d;
public Info() {
}
/* ... insert JavaBean getters/setters here */
}
InfoList.java:
package com.rest.model;
public class InfoList {
private List<Info> infos;
public InfoList() {
}
/* ... insert JavaBean getters/setters here */
}
What this gets serialized to is the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<infos>
<info>
<a>HELLO</a>
<b>WORLD</b>
<c>OLLEH</c>
<d>DLROW</d>
</info>
</streams>
How can I control the way this is serialized? I have tried using annotations, but I only managed to trigger IllegalAnnotationException
s. How would I essentially take this model as it is automatically mapped and express that in annotations? Alternatively, how can I change the fields in the Info
class to be mapped to XML attributes instead?
Upvotes: 2
Views: 2225
Reputation: 148977
By default JAXB will look for the annotations on the public accessors. If you annotate the fields you will see the exception you are getting. If you want to annotate the fields Add the following:
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlAttribute
private String a;
}
For an example see:
Upvotes: 2