You are previewing RESTful Java with JAX-RS.

RESTful Java with JAX-RS

Cover of RESTful Java with JAX-RS by Bill Burke Published by O'Reilly Media, Inc.
O'Reilly logo


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 a simple example:

public class Customer {

    protected int id;

    protected String fullname;

    public Customer() {}

    public int getId() { return; }
    public void setId(int id) { = id; }

    public String getFullName() { return this.fullname; }
    public void setFullName(String name} { 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 @XmlRootElement specifies that our Customer objects should be marshalled into an XML element named <customer> ...

The best content for your career. Discover unlimited learning on demand for around $1/day.