Implementing a JSON resource method using JAXB annotated objects with IBM JSON4J

In WebSphere® Application Server Version 8, the default JSON processor to handle serializing JAXB annotated classes to JSON is Jackson. If you want to use the IBM® JSON4J processor from previous Feature Packs for Web 2.0 instead, you must add a provider class. Jackson offers more advanced JSON processing.

About this task

To use the IBM JSON4J processor in your application, you must add a Java class that extends the JSON4JJAXBProvider class to your application.

Procedure

  1. If you use annotation scanning and do not specify all of the JAX-RS classes through a javax.ws.rs.core.Application subclass, then you must add a Java class that extends the com.ibm.websphere.jaxrs.providers.json4j.JSON4JJAXBProvider class. The following example illustrates a custom JSON4JJAXBProvider class:
    package com.example.jaxrs;
    @Provider
    @Consumes(value = {MediaType.APPLICATION_JSON, "application/javascript"})
    @Produces(value = {MediaType.APPLICATION_JSON, "application/javascript"})
    public class CustomJSON4JJAXBProvider extends
        com.ibm.websphere.jaxrs.providers.json4j.JSON4JJAXBProvider {
    }
  2. If you use a customized javax.ws.rs.core.Application class that lists your JAX-RS classes, you must add your customized JSON4JJAXBProvider to your Application subclass. The following example illustrates a customized Application subclass:
    package com.example.jaxrs;
    public class CustomApplication extends javax.ws.rs.core.Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            /* add your normal JAX-RS classes */
    
            classes.add(CustomJSON4JJAXBProvider.class);
            return classes;
        }
    }

Results

You are now using the IBM JSON4J processor for JAXB-to-JSON serialization instead of the default Jackson processor built in the product.




In this information ...


Related reference

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 of Use | Feedback

Last updatedLast updated: Sep 19, 2011 6:15:55 PM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=matt&product=was-express-dist&topic=twbs_jaxrs_jsoncontent_jaxb_json4j
File name: twbs_jaxrs_jsoncontent_jaxb_json4j.html