Implement 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. To use the IBM JSON4J processor from previous Feature Packs for Web 2.0 instead, add a provider class. Jackson offers more advanced JSON processing.
To use the IBM JSON4J processor in the application, add a Java class that extends the JSON4JJAXBProvider class to the application.
- If we use annotation scanning and do not specify all of the JAX-RS classes through a javax.ws.rs.core.Application subclass, then 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 { }
- If we use a customized javax.ws.rs.core.Application class that lists the JAX-RS classes, add the 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 the normal JAX-RS classes */ classes.add(CustomJSON4JJAXBProvider.class); return classes; } }
Results
We are now using the IBM JSON4J processor for JAXB-to-JSON serialization instead of the default Jackson processor built in the product.
Related tasks
Use JSON content in JAX-RS application requests and responses