Implementing a JSON representation of a resource with Jackson and JAXB annotated objects

The WebSphere® Application Server Feature Pack for Web 2.0 now includes the Jackson JavaScript Object Notation (JSON) processor for helping you read and write JSON.

About this task

This task provides instructions for processing JSON requests and responses with Java Architecture for XML Binding (JAXB) annotated objects and Jackson.

Procedure

  1. Add Jackson into your application if you are running on V6.1, V7.0, or Community Edition of the product. Version 8 of the product includes Jackson in its base runtime environment. Therefore, it is not necessary to add the Java archive (JAR) files. The Jackson JAR files are located in the app_server_root/web2mobilefep_1.1/optionalLibraries/jaxrs_1.X/jackson directory.
  2. In your JAX-RS application subclass, add a custom Java API for RESTful Web Services (JAX-RS) provider for Jackson to your application if you are running on V6.1, V7.0, or Community Edition of the product. The JAX-RS provider is already added by default in V8.0 of the product. The following example illustrates a JAX-RS application subclass:
    public class MyApplication extends javax.ws.rs.core.Application {
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            /* add your other JAX-RS classes */
    
            classes.add(org.apache.winnk.providers.jackson.WinkJacksonJaxbJsonProvider.class);
            return classes;
        }
    }
  3. Create a resource method. For the resource method to return JSON 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. You must also either add an @Produces("application/json") annotation, or set the Content-Type header in your Response object to be "application/json". The following example illustrates simple JSON return methods for the BookList JAXB class:
    @GET
    @Produces("application/json")
    public BookList getBookList() {
        BookList list = /* get a book list */
        return list;
    }
    or
    @GET
    @Produces("application/json")
    public javax.ws.rs.core.Response getBookList() {
        BookList list = /* get a book list */
        return Response.ok(list).type(MediaType.APPLICATION_JSON_TYPE).build();
    }
  4. If you want to return both XML and JSON objects, you can reuse the same method with minimal modification. You must specify "application/xml", "application/json", and "text/xml" in the @Produces annotation. The following example illustrates a simple JSON return method that returns a BookList object for the BookList JAXB class:
    package com.example.jaxrs;
    @GET
    @Produces("application/json", "application/xml", "text/xml")
    public BookList getBookList() {
        BookList list = /* get a book list */
        return list;
    }
    The following example illustrates a simple JSON return method that returns a javax.ws.rs.core.Response object for the BookList JAXB class:
    @GET
    @Produces("application/json", "application/xml", "text/xml")
    public javax.ws.rs.core.Response getBookList() {
        BookList list = /* get a book list */
        return Response.ok(list).build();
    }
    Content negotiation through the Accept HTTP header of the client request determines whether a JSON or XML representation is returned by the resource method. Read about content negotiation for more information. By reusing the JAXB annotated objects, you can shorten the development time to serve both JSON and XML from the same code. Reusing the same model code can be beneficial if specific JSON or XML formatted output is not required.

    If you require specific XML and JSON output and cannot reuse the same JAXB annotated classes, one way to solve the problem is to use two different resource methods. The JSON resource method must be annotated with an @Produces("application/json") annotation and the XML resource method must be annotated with an @Produces("application/xml", "text/xml") annotation.

Results

You have returned a JSON representation with JAXB annotated classes through the Jackson JSON processor.




In this information ...


IBM Redbooks, demos, education, and more

(Index)

Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience.

This feature requires Internet access.

Task topic Task topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Sep 6, 2012 5:50:55 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=v610webmob&product=was-nd-mp&topic=twbs_jaxrs_web20_json_jaxb_annotated_objs
File name: twbs_jaxrs_web20_json_jaxb_annotated_objs.html