File name: twbs_jaxrs_jsoncontent_jaxb_json4j.html
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
- 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 {
}
- 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)
Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof.
|
|
