Detailed explanation of the use of JAXB annotations

1. What is JAXB?

JAXB (that is, Java Architecture for XML Binding) is an industry standard, that is, a technology that can generate Java classes according to XML Schema. In this process, JAXB also provides a method to reverse the XML instance document to generate the Java object tree, and can rewrite the content of the Java object tree to the XML instance document.

2. Annotation introduction

1. Commonly used annotations in Jaxb to deal with the conversion between java and xml are:

1)        @XmlType

2)        @XmlElement

3)        @XmlRootElement

4)        @XmlAttribute

5)        @XmlAccessorType

6)        @XmlAccessorOrder

7)        @XmlTransient

8)        @XmlJavaTypeAdapter

1. Instructions for use of common annotations

1)      @XmlType

This annotation is used on class classes and is often used together with @XmlRootElement and @XmlAccessorType. It has three properties: name, propOrder, namespace, only the first two properties are often used. Such as:

[java] view plain copy
  1. @XmlType(name = "basicStruct", propOrder = {"intValue","stringArray","stringValue"  
  2. )  
  3. Public class myType{…}  

When using the propOrder property of @XmlType , all properties in the JavaBean object must be listed (and xml annotations should be added to all properties), otherwise an error will be reported.

2)      @XmlElement

This annotation is used on properties of java classes to map properties to child nodes of xml. The name of the java attribute in the xml file can be changed by configuring the value of the name attribute later. Such as:

[java] view plain copy
  1. @XmlElement(name="Address")    
  2. private String yourAddress;  

After configuring here, the node in the xml file is <Address></Address> instead of <yourAddress/>.

3)      @XmlRootElement

Class-level annotations correspond to the root node in the xml file. Often used with @XmlType and @XmlAccessorType. Such as:

[java] view plain copy
  1. @XmlType  
  2. @XmlAccessorType(XmlAccessType.FIELD)  
  3. @XmlRootElement  
  4. publicclass Address {}   

4)      @XmlAttribute

It is used to map the properties of the java object to the properties of xml, and the generated xml properties can be assigned aliases through the name property. Such as:

[java] view plain copy
  1. @XmlAttribute(name="Country")  
  2. private String state;  

5)      @XmlAccessorType

  @XmlAccessorType is used to specify the access method to the properties of the java object when the xml file is generated by the java object. Often used with @XmlRootElement, @XmlType. Its attribute value is the four enumeration values ​​of XmlAccessType, which are:

  XmlAccessType.FIELD: All member variables in the java object;

  XmlAccessType.PROPERTY: All member variables in the java object accessed through getter/setter;

  XmlAccessType.PUBLIC_MEMBER: All public access member variables in the java object and member variables accessed through getter/setter;

  XmlAccessType.NONE: All properties of java objects are not mapped to elements of xml.

Note: The default access level for @XmlAccessorType is XmlAccessType.PUBLIC_MEMBER. Therefore, if the private member variable in the java object is set with the getter/setter method of public permission , do not use the @XmlElement and @XmlAttribute annotations on the private variable , otherwise the same attribute will be reported in the java class when the xml is generated from the java object There are two errors in it . Similarly, if the access authority of @XmlAccessorType is XmlAccessType.NONE, if the @XmlElement or @XmlAttribute annotations are used on the member variables of java, these member variables can still be mapped to the xml file.

6)      @XmlAccessorOrder

Used to sort xml elements generated by java objects. It has two property values:

  AccessorOrder.ALPHABETICAL: sort the generated xml elements alphabetically;

 XmlAccessOrder.UNDEFINED: Do not order.

7)      @XmlTransient

Used to indicate that this attribute is ignored when mapping xml by a java object. That is, this element does not appear in the generated xml file.

8)      @XmlJavaTypeAdapter

  @XmlJavaTypeAdapter is often used to convert more complex objects, such as map types or formatted dates. When using this annotation, you need to write an adapter class to inherit the XmlAdapter abstract class and implement the methods inside.

  @XmlJavaTypeAdapter(value=xxx.class), value is the adapter class defined by itself

The XmlAdapter is as follows:

[java] view plain copy
  1. publicabstractclass XmlAdapter<ValueType,BoundType> {    
  2.     // Do-nothing constructor for the derived classes.  
  3.     protected XmlAdapter() {}  
  4.     // Convert a value type to a bound type.  
  5.     publicabstract BoundType unmarshal(ValueType v);   
  6.     // Convert a bound type to a value type.  
  7.     publicabstract ValueType marshal(BoundType v);   
  8.  } 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324626982&siteId=291194637