Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in my Class do I put the @XmlElement Annotation?

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.

like image 415
JSK NS Avatar asked Nov 05 '13 00:11

JSK NS


People also ask

What does @XmlElement do in Java?

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.

What is@ XmlElement annotation?

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.

What is @XmlType annotation in Java?

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.

What is the use of @XmlRootElement annotation?

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 .


1 Answers

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

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
like image 144
bdoughan Avatar answered Sep 20 '22 00:09

bdoughan