Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


JAXB Annotations

JAXB stands for Java Architecture for XML Binding. It offers the possibility to convert Java objects into XML structures and the other way around. JAXB defines an API for reading and writing Java objects to and from XML documents. Although JSON is not XML and XML is not JSON, but the process for (de)serializing Java objects to/from JSON has many similarities to the process of (de)serializing Java objects to/from XML. So a significant part of the JAXB metadata is applicable to (de)serializing JSON data.
In this post will examine the different options JAXB offers for representing collections in XML. We will look at the following annotations:


@XmlRootElement -Define the root element for the XML to be produced with @XmlRootElement JAXB annotation. The name of the root XML element is derived from the class name.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Customer {


}

The following is the corresponding XML output:

<customer>
</customer>

@XmlAccessorType -You can annotate a package or a top level class with XmlAccessorType, setting its value element to one of the enum constants FIELD, PROPERTY, PUBLIC_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.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {


}

The following is the corresponding XML output:

<customer>
</customer>

@XmlElement - We can control the name of the XML element a collection item is marshalled to by using the @XmlElement annotation. This annotation maps a property contained in a class to a local element in the XML Schema complex type to which the containing class is mapped.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

@XmlElement(name="email-address")
private List<String> emailAddresses;

}

The following is the corresponding XML output:

<customer>
<email-address>email@test.com</email-address>
<email-address> email@test.org</email-address>
</customer>

@XmlElementWrapper -This annotation can be use to generate a wrapper element around an XML element representation. When no name defined, the @XmlElementWrapper annotation uses the property name as the wrapper element name.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

@XmlElementWrapper(name="email-addresses")
@XmlElement(name="email-address")
private List<String> emailAddresses;

}

The following is the corresponding XML output:

<customer>
<email-addresses>
<email-address>email@test.com</email-address>
<email-address> email@test.org</email-address>
<email-addresses>
</customer>

@XMLMimeType -Associates the MIME type that controls the XML representation of the property with a textual representation, such as image/jpeg.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Customer {

@XmlMimeType("image/jpeg")
private Image image;

}


@XmlType -@XmlType annotation to control the ordering of XML elements.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"ID", "name", "address"})
public class Customer {

private String address;
private String name;
private String ID;

}


Below is the XML output based on the above model.

<customer>
<ID>123</ID>
<name>Jane</name>
<address>Doe</address>
</customer>


@XmlAttribute - An annotation stating that the annotated java field should be mapped to a xml attribute during XML serialization and deserialization.

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"ID", "name", "address"})
public class Customer {

@XmlAttribute
private String address;
private String name;
private String ID;

}


@XmlValue - javax.xml.bind.annotation.XmlValue map an object to an XML element with attributes and text in JAXB. It Enables mapping a class to a XML Schema complex type with a simpleContent or a XML Schema simple type. The @XmlValue annotation can be used with the following program elements:

  • a JavaBean property.
  • non static, non transient field.

The usage is subject to the following usage constraints:

  • At most one field or property can be annotated with the @XmlValue annotation.
  • @XmlValue can be used with the following annotations: XmlList. However this is redundant since XmlList maps a type to a simple schema type that derives by list just as XmlValue would.
  • If the type of the field or property is a collection type, then the collection item type must map to a simple schema type.
  • If the type of the field or property is not a collection type, then the type must map to a XML Schema simple type.

If the annotated JavaBean property is the sole class member being mapped to XML Schema construct, then the class is mapped to a simple type. If there are additional JavaBean properties (other than the JavaBean property annotated with @XmlValue annotation) that are mapped to XML attributes, then the class is mapped to a complex type with simpleContent. It is important to note that the @XmlValue annotation can not be used with a class that maps any of its fields/properties to XML elements.

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"ID", "name", "address"})
public classSubject

@XmlValue
private String Subject;

@XmlValue(name = "subjectType", required = true)
private String subjectType;
public String getSubject() {
return StandardPrice;
}

public void setSubject(String value) {
StandardPrice = value;
}

public void setSubjectType(String currency) {
this.currency = currency;
}

public String getSubjectType() {
return currency;
}

}


In above code we used @XmlValue to denote that this is used to add property value not a new xml tag. If we do not define this, then it will produce one extra <subject> tag with in a main <subject> tag. @XmlAttribute(name = "subjectType", required = true) here we are defining that property name will be subjectType and it is a required field. Method getSubjectType() will return the value of subjectType property and getSubject() method will return the value of subject tag in XML.



@XmlSeeAlso - @XmlSeeAlso annotation is used to include custom classes (sub classes) inside client stubs (proxies). This annotation will modify the WSDL file and add a complex type automatically. By doing that, clients will have a complete stub classes . Lets examine the following scenario; public abstract class Product() {} public class Phone extends Product() {} public class Computer extends Product() {}

import javax.xml.bind.annotation.*;

abstract class Product() {}

class Phone extends Product() {}

class Computer extends Product() {}

@WebService
@XmlSeeAlso({packagename.Phone.class, packagename.Computer.class})
public class MainWebService {

@WebMethod(operationName = "getTheProduct")
public Product getTheProduct() {

Product product = new Phone();

//if you use XmlSeeAlso annotation
//phone class can be created and returned without an error
//otherwise it will throw an exception in client proxies
//and JAX-WS will only create a Product class in client proxy
return product;

}
}

}


All subclasses of the Product class (such as Phone and Computer) will be included inside client proxies so that any kind of casting can be done in these proxies. However if you do not include XMLSeeAlso annotation to your WebService, only the Product class will be included by JAXB and this won't allow developers to use Phone or Computer classes' fields and methods inside client proxies.


     << Previous
Next >>