If JAXB binds a class to XML, then, by default, all public members will be bound, i.e., public getter and setter pairs, or public fields. Any protected, package-visible or private member is bound if it is annotated with a suitable annotation such as XmlElement or XmlAttribute. You have several possibilities to influence this default behaviour.

You can annotate a package or a top level class with XmlAccessorType, setting its value element to one of the enum constants FIELDPROPERTYPUBLIC_MEMBER or NONE. If FIELD is set every non static, non transient field will be automatically bound. PROPERTY instructs JAXB to do this for getter and setter pairs. NONE suppresses bind except for explicitly annotated fields or properties. A class without this annotation inherits the XmlAccessorType setting either from its superclass or from the package setting.

The other annotation to be mentioned in this context is XmlTransient. It suppresses binding for its target which can be an entire class or a field or a method. This is also useful if you have a name clash resulting from a public field, say fooand methods getFoo and setFoo.

The first class illustrates a class that restricts the set of XML elements from an accessor type setting of PUBLIC_MEMBER. Member getB is blocked from being bound.

@XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
public class SomeClass {
private String a;
private String b; public SomeClass(){ ... } public String getA(){ ... }
public void setA( String value ){ ... } @XmlTransient
public String getB(){ ... }
public void setB( String value ){ ... }
}

The corresponding XML schema type definition looks like this:

<xs:complexType name="someClass">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

The second example illustrates the reverse process. It shows a class with the most restrictive accessor type setting, with one member explicitly annotated as an element.

@XmlAccessorType( XmlAccessType.NONE )
public class OtherClass {
private String a;
private String b; public OtherClass(){ ... } public String getA(){ ... }
public void setA( String value ){ ... } @XmlElement( required = true )
public String getB(){ ... }
public void setB( String value ){ ... }
}

Since we have set the annotation element required to true, the generated schema snippet is slightly different:

<xs:complexType name="otherClass">
<xs:sequence>
<xs:element name="b" type="xs:string"/>
</xs:sequence>
</xs:complexType>

The final example for this topic demonstrates using these annotations in somewhat special circumstances. First, XmlTransient is used on the public field to avoid the name clash with the method pair. Second, XmlElement is used to request binding for getB, which doesn't have its setB spouse. (The getter follows the standard pattern of the JAXB generated Java code for elements bound to List<?>, with changes being made on the list object.)

@XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
public class SpecialClass {
@XmlTransient
public String a;
private List<String> b; public SpecialClass(){ ... } public String getA(){ ... }
public void setA( String value ){ ... } @XmlElement
public List<String> getB(){
if( b == null ) b = new ArrayList<String>();
return b;
}
}

The generated complex type features both elements.

<xs:complexType name="specialClass">
<xs:sequence>
<xs:element name="a" type="xs:string" minOccurs="0"/>
<xs:element name="b" type="xs:string" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

Taken together, this means that you can, either at package level or at some superclass, define the strategy for all classes within the package or for all subclasses, respectively. This strategy may be generally permissive, oriented on fields or properties, or restrictive, permitting nothing by default. Within a class, you may extend a restrictive setting by adding XmlElement or XmlAttribute, or you may inhibit bindings using the XmlTransient annotation.

JAXB - Annotations, Controlling Element Selection: XmlAccessorType, XmlTransient的更多相关文章

  1. JAXB - Annotations, The Annotation XmlElement

    The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...

  2. JAXB - Annotations, Top-level Elements: XmlRootElement

    A class that describes an XML element that is to be a top-level element, i.e., one that can function ...

  3. JAXB - Annotations, Annotations for the Schema: XmlSchema

    This annotation can only be used with a package. It defines parameters that are derived from the xsd ...

  4. JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl

    To be able to create objects from XML elements, the unmarshaller must have an object factory with me ...

  5. JAXB - Annotations, Class Fields as Attributes: XmlAttribute

    Provided that XML lets you represent a data item as a single value, there is no cut-and-dried rule f ...

  6. JAXB - Annotations, Type Mapping: XmlSchemaType

    The annotation XmlSchemaType defines a mapping between an arbitrary Java type and a simple schema bu ...

  7. JAXB - Annotations, Annotation for Classes: XmlType

    This annotation adds information that would be available from a schema type, but isn't implied by a ...

  8. JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue

    An enum type is annotated with XmlEnum. It has an optional element value of type java.lang.Class whi ...

  9. JAXB - Annotations, Type Adapters: XmlJavaTypeAdapter

    For some Java container types JAXB has no built-in mapping to an XML structure. Also, you may want t ...

随机推荐

  1. Eclipse热部署JSP

    项目中JSP修改,每次都要重新部署启动才生效,我真切记得以前都不是这样子的 本来应该只需要刷新页面就可以查看到最新的修改的了 和以前工作空间项目配置对比        唯一不同的是,我现在这个工作空间 ...

  2. Xcode5和ios7下交叉编译ffmpeg

    先申明本机环境 dev-mini:ffmpeg devone$ sw_vers  ProductName:    Mac OS X ProductVersion:  BuildVersion:   1 ...

  3. RDMA编程实例

    1,RDMA verbs Multicast Code for Multicast Using RDMA_CM(Remote directory memory access_connect manag ...

  4. MySQL Workbench 导出数据库脚本(图文)

    1.如下图红框所示,在Server Administration那里,点击"New Server Instance" 2.如下图所示,如果是连接本地数据库,则选localhost, ...

  5. STM32 SysTick定时器应用【worldsing笔记】

    SysTick是CM内核独立的定时器,时钟可以用内核内部的,也可以用芯片厂家(ST)的时钟,参考<Cortex-M3权威指南>的第13章: 另外也可以考<STM32F10xxx Co ...

  6. gtest框架使用

    gtest文档说明: 由于公司单元测试的需要,自己花了大半天时间下载了一个gtest框架,使用了一些测试例子,总览了coderzh的玩转gtest测试框架,又看了几篇gtest博客,写下了以下内容,作 ...

  7. hdu 4612 (双联通+树形DP)

    加一条边后最少还有多少个桥,先Tarjan双联通缩点, 然后建树,求出树的直径,在直径起点终点加一条边去的桥最多, #pragma comment(linker, "/STACK:10240 ...

  8. Windows下环境变量配置

    JAVA_HOME=C:\Program Files\Java\jdk1.6.0_33   PATH+=%JAVA_HOME%\bin;   CLASSPATH=.;%JAVA_HOME%\lib\d ...

  9. ng-cookie 的基本使用

    2.angular-cookie - 配置$cookiesProvider ```angular.module("Demo",[]).config(["$cookiesP ...

  10. Oracle 监听器无法启动(TNS-12555,TNS-12560,TNS-00525)

    启动监听器无法打开,报错! [oracle@localhost ~]$ lsnrctl start LSNRCTL for Linux: Version 11.2.0.1.0 - Production ...