This example demonstrates how to produce/consume JSON representation from Java objects. This applies not only to JAXB beans, as shown at json-from-jaxb example but also to ordinary, unannotated, POJOs
A simple web application is presented with two simple read-only resources: One is based on a simple unannotated POJO, and could only provide a JSON representation. The second one shows how to workaround the default Jersey JSON provider limitation regarding empty arrays in JAXB beans. For this resource, the JSON representation is produced by Jackson JAX-RS provider, while XML representation is still generated by JAXB
The "empty array" web resource is implemented by com.sun.jersey.samples.jacksonjsonprovider.EmptyArrayResource
class.
The "non JAXB" web resource is implemented by com.sun.jersey.samples.jacksonjsonprovider.NonJAXBBeanResource
class.
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 |
Please note the two files in src/main/java/META-INF/services
directory (javax.ws.rs.ext.MessageBodyReader
and javax.ws.rs.ext.MessageBodyWriter
),
which are responsible for the Jackson JAX-RS provider registration.
You will also need to define the jackson-jaxrs
dependency in your POM. See the pom.xml
file for details.
Run the example as follows:
mvn clean compile exec:java
This deploys the JSON from JAXB example using Grizzly
A WADL description may be accessed at the URL:
http://localhost:9998/jacksonjsonprovider/application.wadl
The two resources could be accessed via
http://localhost:9998/jacksonjsonprovider/emptyArrayResource
http://localhost:9998/jacksonjsonprovider/nonJAXBResource