File name: twbs_jaxrs_xmlcontent_jaxb.html
Implementing a resource method using JAXB objects for XML content
You can process XML in requests and responses using Java
Architecture for XML Binding (JAXB) annotated classes.
About this task
You can use JAXB objects as request entity parameters
and response entities with Java™ API
for RESTful Web Services (JAX-RS) applications. Instead of transforming
XML to and from native Java types in a tedious manner, you can leverage
the advantages of using JAXB objects. Even though you can use the
javax.xml.transform.Source, java.io.InputStream, or java.lang.String
interfaces or even a simple byte array to store the XML either as
a request or response entity, JAXB enables easier data binding
The
JAX-RS runtime environment has standard MessageBodyReader and MessageBodyWriter provider
interfaces for reading and writing JAXB objects as entities.
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 by the application using a JAX-RS ContextResolver provider
interface.
Procedure
- Create a resource method.
For the resource
method to return XML content, return an instance of a JAXB class directly
or return a javax.ws.rs.core.Response object with a JAXB object as
the response entity.
Suppose that BookList is a JAXB class;
for example:
@GET
@Produces("application/xml", "text/xml")
public BookList getBookList() {
BookList list = /* get a book list */
return list;
}
or
@GET
@Produces("application/xml", "text/xml")
public javax.ws.rs.core.Response getBookList() {
BookList list = /* get a book list */
return Response.ok(list).build();
}
In this example,
application/xml and
text/xml are
produced from the resource methods
- Define the entity parameter in the resource method using
a JAXB class to process XML in a request entity.
Suppose
that Book is a JAXB class; for example:
@POST
@Consumes("application/xml", "text/xml")
@Produces("application/xml", "text/xml")
public Book createBook(Book aBook) {
/* store aBook into a data store */
return aBook;
}
- (optional) If the JAX-RS runtime environment is not able
to create an appropriate JAXBContext instance, you can create a class
that implements the javax.ws.rs.ext.ContextResolver interface.
This scenario might occur because of complexities of the JAXB
classes for the application. You must add the @Provider annotation
to the class; for example:
@Provider
public class MyContextResolver implements ContextResolver<JAXBContext> {
public JAXBContext getContext(Class<?> aType) {
if(aType == Book.class) {
JAXBContext myContext = /* This statement creates create a JAXB Context. */
return myContext;
}
/* This method returns null for any types not understood.
If null is returned, other application supplied ContextResolver<JAXBContext>
will be used if available */
return null;
}
}
Add this class to the set of classes to return in your
application sub-class, just as you did with your resource classes.
Results
You have configured your JAX-RS Web application to use
JAXB classes to consume and produce XML.
In this information ...
| IBM Redbooks, demos, education, and more(Index)
Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof.
|
|
