In my JAXB classes, do I put the @XmlElement annotation above the private variable declaration?
@XmlElement(name = "report_name")
private String name;
Above the setter?
@XmlElement(name = "report_name")
public void setName(String name) {
this.name = name;
}
Or above the getter?
@XmlElement(name = "report_name")
public String getName() {
return name;
}
I've read through several JAXB tutorials and have yet to find a consistent pattern.
A JavaBean property, when annotated with @XmlElement annotation is mapped to a local element in the XML Schema complex type to which the containing class is mapped. Example 2: Map a field to a nillable element. Example 3: Map a field to a nillable, required element.
Guideline: The @XmlElement annotation maps a property or field to an XML element. This is also the default mapping in the absence of any other JAXB 2.0 annotations. The annotation parameters in @XmlElement can be used to specify whether the element is optional or required, nillable or not.
If class is annotated with @XmlType(name="") , it is mapped to an anonymous type otherwise, the class name maps to a complex type name. The XmlName() annotation element can be used to customize the name. Properties and fields that are mapped to elements are mapped to a content model within a complex type.
When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .
By default JAXB impls treat public fields and properties as mapped. You can put the annotation on the get or set method and it will be treated the same. If you want to annotate the field you should specify @XmlAccessorType(XAccessType.FIELD)
on the class.
For More Information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With