Network Deployment (Distributed operating systems), v8.0 > End-to-end paths > Web services - RESTful services > Use XML content in JAX-RS application requests and responses > 4. Implement a resource method using JAXB objects for XML content.


Implement a resource method using JAXB objects for XML content

We can process XML in requests and responses using Java Architecture for XML Binding (JAXB) annotated classes.

We 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 class for the application using a JAX-RS ContextResolver provider interface.


Procedure

  1. 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.
  2. 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;
    }
    
  3. (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. We 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 content.
Use XML content in JAX-RS application requests and responses
Use JAXB for XML data binding

+

Search Tips   |   Advanced Search