Java Reference
In-Depth Information
As you define the resources for your application, consider the type of data you want to
expose. You may already have a relational database that contains information you want
to expose to users, or you may have static content that does not reside in a database but
does need to be distributed as resources. Using JAX-RS, you can distribute content from
multiple sources. RESTful web services can use various types of input/output formats for
request and response. The customer example, described in “ The customer Example
Application on page 209 , uses XML.
Resources have representations. A resource representation is the content in the HTTP
message that is sent to, or returned from, the resource using the URI. Each representation
a resource supports has a corresponding media type. For example, if a resource is going
to return content formatted as XML, you can use application/xml as the associated
media type in the HTTP message. Depending on the requirements of your application,
resources can return representations in a preferred single format or in multiple formats.
JAX-RS provides @Consumes and @Produces annotations to declare the media types
that are acceptable for a resource method to read and write.
JAX-RS also maps Java types to and from resource representations using entity providers.
A MessageBodyReader entity provider reads a request entity and deserializes the re-
quest entity into a Java type. A MessageBodyWriter entity provider serializes from a
Java type into a response entity. For example, if a String value is used as the request en-
tity parameter, the MessageBodyReader entity provider deserializes the request body
into a new String . If a JAXB type is used as the return type on a resource method, the
MessageBodyWriter serializes the JAXB object into a response body.
By default, the JAX-RS runtime environment attempts to create and use a default
JAXBContext class for JAXB classes. However, if the default JAXBContext class is
not suitable, then you can supply a JAXBContext class for the application using a JAX-
RS ContextResolver provider interface.
The following sections explain how to use JAXB with JAX-RS resource methods.
Using Java Objects to Model Your Data
If you do not have an XML schema definition for the data you want to expose, you can
model your data as Java classes, add JAXB annotations to these classes, and use JAXB to
generate an XML schema for your data. For example, if the data you want to expose is a
collection of products and each product has an ID, a name, a description, and a price, you
can model it as a Java class as follows:
Search WWH ::




Custom Search