Jackson JAX-RS JSON Provider Example

This example demonstrates how to produce/consume JSON representations from Java objects. This applies not only to JAXB beans, as shown in the json-from-jaxb example but also to ordinary, un-annotated, POJOs.

This example hosts three simple read-only resources: One provides an example of using a Jackson JSON provider (set by the feature JSONConfiguration.FEATURE_POJO_MAPPING in the Main class) instead of using JAXB (Object->JAXB->JSON) which has some limitations (e.g. empty arrays in JAXB beans). For this resource the JSON representation is produced by the Jackson JAX-RS provider, while the XML representation is generated by JAXB as usual. The second web resource is based on a simple un-annotated POJO and provides only JSON-based representations: JSON and JSON with padding (JSONP). The third resource depicts how Jackson and JAXB annotations could be mixed together within a single POJO.

Contents

The "empty array" web resource is implemented by the com.sun.jersey.samples.jacksonjsonprovider.EmptyArrayResource class. The "non JAXB" web resource is implemented by the com.sun.jersey.samples.jacksonjsonprovider.NonJAXBBeanResource class. Both resources use the default Jackson mapper configuration to serialize JSON data out and depicts the corner cases, where the Jersey internal, StAX based, JSON processing can not be utilized.

The com.sun.jersey.samples.jacksonjsonprovider.CombinedAnnotationResource class is used to show how JAXB and Jackson annotations could be combined together in one Java bean for JSON serialization. See the com.sun.jersey.samples.jacksonjsonprovider.MyObjectMapperProvider class where Jackson specific options are used to set up the desired JSON serialization configuration.

The mapping of the URI path space is presented in the following table:

URI path Resource class HTTP methods
/emptyArrayResource EmptyArrayResource GET
/nonJAXBResource NonJAXBBeanResource GET
/combinedAnnotations CombinedAnnotationResource GET

Please note the JSONConfiguration.FEATURE_POJO_MAPPING feature set with the init parameters in the example Main class. This feature enables the Jackson JAX-RS provider. If this feature is set to false or is missing, the Jackson JAX-RS providers will not be utilized.

To use Jackson specific configuration options, one can implement a ContextResolver<ObjectMapper> provider. For an example of such an implementation, see the MyObjectMapperProvider class.

Running the Example

Run the example as follows:

mvn clean compile exec:java

This deploys the example using Grizzly

A WADL description may be accessed at the URL:

http://localhost:9998/jacksonjsonprovider/application.wadl

The three resources are available at

http://localhost:9998/jacksonjsonprovider/emptyArrayResource
http://localhost:9998/jacksonjsonprovider/nonJAXBResource
http://localhost:9998/jacksonjsonprovider/combinedAnnotations

To easily obtain the different output types available, cURL can be used as follows:

Obtain the JSON output of EmptyArrayResource (use -HAccept:application/xml for XML output):

curl -HAccept:application/json http://localhost:9998/jacksonjsonprovider/emptyArrayResource

Obtain the JSON output of NonJAXBBeanResource (use -HAccept:application/javascript for JSONP output):

curl -HAccept:application/json http://localhost:9998/jacksonjsonprovider/nonJAXBResource

Obtain the JSON output of CombinedAnnotationResource:

curl -HAccept:application/json http://localhost:9998/jacksonjsonprovider/combinedAnnotations