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.


指出主題類型的圖示 作業主題



時間戳記圖示 前次更新: July 9, 2016 11:18
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_jaxrs_jsoncontent_jaxb_json4j
檔名:twbs_jaxrs_jsoncontent_jaxb_json4j.html