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. DateTime和DateTime2

    1.与ANSI和ISO8601标准的一致性不同 datetime不符合该标准,datetime2符合该标准.对于新的应用,尽量使用符合标准的类型. 2.表示范围的不同 datetime:1753-01 ...

  2. HDU-4607 Park Visit bfs | DP | dfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...

  3. openstack fe

  4. 八成Java开发者解答不了的问题

    统计数据来自Java“死亡”竞赛——一个针对开发者的迷你测验 几个月前,我们在一个小型网站上发布了一个称为Java“死亡竞赛”的新项目.测验发布后,超过20000位开发者参加了测验.网站以20道关于J ...

  5. DTD - XML Building Blocks

    The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...

  6. 弹出框JBox实例

    前几天做的考试系统的一些后台弹出框的一些模板.主要是因为普通的弹出框样式不是很好,颜色也不能调换.这里我们用的是JBox,还是从师傅那得知的.自己小实验了下,这里就做个小结. JBox 插件说明 - ...

  7. LDO稳压器工作原理

    LDO稳压器工作原理 随着便携式设备(电池供电)在过去十年间的快速增长,像原来的业界标准 LM340 和LM317 这样的稳压器件已经无法满足新的需要.这些稳压器使用NPN 达林顿管,在本文中称其为N ...

  8. 【Stage3D学习笔记续】真正的3D世界(四):空间大战雏形

    前面几个星期抽空用Starling做了一个打飞机的小游戏(所以没有接着看书了),准备面试时用的,结果面试还是没过%>_<%...这个游戏打算过几天全部开源了 那么接下来打算这周把<S ...

  9. EventBus的使用,初学EventBus传值

    一.概述 EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间 ...

  10. 实例:ABAP Tree Control 使用与ALV Grid对象关联

    Tree Control 是最常用的Windows控件之一,在其他语言中成为"Tree View"等,ABAP的 Tree Contiol 能实现类似的功能. 本文主要介绍一下内容 ...