| ||||||||||||||||||||||||||||
Resin 3.1 Documentation Examples Changes Overview Installation Configuration Quercus SOA/IoC JSP Servlets and Filters Admin (JMX) EJB Amber Security Performance Hessian XML and XSLT Third-party Troubleshooting/FAQ JAXB Annotations IoC Annotations JAX-WS Annotations JMS Configuration init bean CronResource RMI Resource FAQ |
JAXB annotations customize the serialization of a model bean. @XmlAccessorType @XmlAccessorType sets default field and property serializability. By default, JAXB serializes public fields and properties. By setting @XmlAccessorType, the bean can choose to only allow annotated fields to be serialized. @XmlAccessorType works with the other annotations and @XmlTransient to serialize fields and properties. @XmlTransient prevents serialization, overriding the @XmlAccessorType. The presense of any other annotation will force serialization, overriding the @XmlAccessorType.
@XmlAccessorType(XmlAccessType.FIELD) class Bean { private String data; } <Bean> <data>Sample Data</data> </Bean>
@XmlAttribute @XmlAttribute marks a field or property as serialized to an XML attribute. By default, fields serialize to XML elements. It can also customize the XML attribute name and namespace. @XmlAttribute can work with @XmlAccessorType to select which fields and properties should be serialized to XML. By default, public fields and properties will be serialized. Adding @XmlAttribute to a private field will mark that field as serializable. @XmlAttribute can also customize the XML attribute name. By default, the
XML attribute name is taken from the field name or the property name.
The @XmlRootElement class Bean { @XmlAttribute("sample-field") private String _myField; } <Bean sample-field="A sample value"> </Bean>
@XmlElement @XmlElement marks a field or property as serialized to an XML element. It can also customize the XML element name and namespace. @XmlElement can work with @XmlAccessorType to select which fields and properties should be serialized to XML. By default, public fields and properties will be serialized. Adding @XmlElement to a private field will mark that field as serializable. @XmlElement can also customize the XML element name. By default, the
XML element name is taken from the field name or the property name.
The @XmlRootElement class Bean { @XmlElement("sample-field") private String _myField; } <Bean> <sample-field>A sample value</sample-field> </Bean>
@XmlElements @XmlElements allows lists to contain multiple different tags. It contains a list of @XmlElement values allowed as values. class Bean { @XmlElements({ @XmlElement(name="a",type=BeanA.class), @XmlElement(name="b",type=BeanB.class) }) private List<SubBean> data = new List<SubBean>(); } class BeanA extends SubBean { @XmlValue private String data; } class BeanB extends SubBean { @XmlValue private String data; } <Bean> <a>Some BeanA Data</a> <b>Some BeanB Data</b> <a>Another BeanA Data</a> </Bean>
@XmlElementWrapper @XmlElementWrapper adds a wrapper XML tag for list values. By default, JAXB list values are serialized directly without any extra tags. @XmlElementWrapper adds a container XML tags. class Bean { @XmlElementWrapper(name="values") private List<String> data = new ArrayList<String>(); } <Bean> <values> <data>Some data</data> <data>Another item</data> <data>Third item</data> </values> </Bean>
@XmlJavaTypeAdapter @XmlJavaTypeAdapter specifies a Java class which converts helper values to final values. In some cases, the Java model may not directly match the XML model. For example, it's complicated to represent Java maps in XML. The @XmlJavaTypeAdapter provides a standard way of managing complex types. class Bean { @XmlJavaTypeAdapter(MyMapAdapter.class) private HashMap<String,String> map; } class MyMapAdapter extends XmlAdapter<Temp,Map<String,String>> { ... } class Temp { @XmlElement private List<Item> entry = new ArrayList<item>(); } class Item { @XmlAttribute private String key; @XmlAttribute private String value; } <Bean> <entry key="a" value="data-a"/> <entry key="b" value="data-b"/> <entry key="c" value="data-c"/> </Bean>
@XmlRootElement @XmlRootElement marks a class as a top-level XML node. @XmlRootElement can also be used with @XmlElementRef to handle some inheritance situations. The @XmlRootElement(name="my-bean") class Bean { public String data; } <my-bean> <data>A sample value</data> </my-bean>
@XmlTransient @XmlTransient marks a field or property as unserializable. JAXB will ignore the transient field.
@XmlValue @XmlValue marks a single field as representing the entire content of the bean. If a bean has an @XmlValue annotation, no other property or field may be serialized. class Bean { @XmlValue private String data; } <Bean>Sample Data</Bean>
|