Java Reference
In-Depth Information
JAXB is an older Java specification and is not defined by JAX-RS. JAXB is an annotation
framework that maps Java classes to XML and XML schema. It is extremely useful because
instead of interacting with an abstract representation of an XML document, you can work
with real Java objects that are closer to the domain you are modeling. JAX-RS has built-in
support for JAXB, but before we review these handlers, let's get a brief overview of the
JAXB framework.
Intro to JAXB
A whole book could be devoted to explaining the intricacies of JAXB, but I'm only going to
focus here on the very basics of the framework. If you want to map an existing Java class to
XML using JAXB, there are a few simple annotations you can use. Let's look at an example:
@XmlRootElement ( name = "customer" )
@XmlAccessorType ( XmlAccessType . FIELD )
public class
class Customer
Customer {
protected int
int id ;
protected String fullname ;
public Customer () {}
public int
int getId () { return
return this
this . id ; }
public void
void setId ( int
int id ) { this
this . id = id ; }
public String getFullName () { return
return this
this . fullname ; }
public void
void setFullName ( String name } { this
this . fullname = name ; }
The @javax.xml.bind.annotation.XmlRootElement annotation is put on Java classes to
denote that they are XML elements. The name() attribute of @XmlRootElement specifies the
string to use for the name of the XML element. In our example, the annotation @Xm-
lRootElement specifies that our Customer objects should be marshalled into an XML ele-
ment named <customer> .
The @javax.xml.bind.annotation.XmlAttribute annotation was placed on the id field
of our Customer class. This annotation tells JAXB to map the field to an id attribute on the
main <Customer> element of the XML document. The @XmlAttribute annotation also has a
Search WWH ::

Custom Search