Oracle Blog

Blog for japod

Friday Feb 13, 2009

This is an update for a tech tip on configuring JSON in Jersey, which i wrote in October 2008. The way of JSON configuration, suggested in the tech tip, is now deprecated (but still functioning). Here i would like to describe the new API, which will hopefully last (and be supported) a way longer.

Notice: you will need to bundle jaxb-impl-2.1.10.jar
with your application in order to take advantage of the recently added JSON NATURAL convention

Deprecated Configuration

Configuring JSON format, as described in the tech tip, meant to implement a JAXBContext resolver
class returning an instance of JSONJAXBContext. This principle have not changed.
What changed is a way, how the JSONJAXBContext itself is being configured.
Lets look at the sample code below (using the deprecated API):

In the currently available 1.0.2 Jersey version, a new JSONConfiguration class was introduced to became a central point for JSON configuration options.
For creating a new JSONConfiguration instance, a builder pattern is employed.
It is not only more user friendly, but also ensures only meaningful JSON options could be combined together.
You can compare the following code, with the deprecated example above:

Further Simplification

If you go a bit further, you can ask if the configuration could be simplified even more.
Imagine you have much bigger number of JAXB beans in your model, and they are more complex.
It could easily become unmanageable to maintain a reasonable JSON configuration as described so far.
Then if you happen to have conflicting non-string/string values and/or arrays/non-arrays elements in your set,
you could easily run out of options there.

A natural way to overcome above mentioned issues, is to simply use recently introduced JerseyNATURAL JSON notation. Then you need only to:

Such configuration is simple from user point of view, but yet very powerful. You do not need to keep
various configuration options in sync with your actual JAXB beans, and be worried what options to actually use
(what exact names, etc.). Jersey will automatically take care about
serializing Java collections/arrays as JSON arrays, Java booleans as JSON booleans, Java ints as JSON integers,and so on.

Monday May 26, 2008

Having a JSON generating REST resource, you can consume provided
data in your web page using JavaScript pretty easily. To access data
at your own site you can obviously make a HttpRequest from JavaScript code.
To access data from another site, you will need to workaround
a cross-domain restriction somehow. Two possible approaches are described
at Dan Theurer's blog entries here and here.

In this entry i will show how to add the JavaScript representation option to your Jersey based REST resource, so that besides

{ some JSON data}

when your client asks for http://.../myResource.json,
you will be able to return also something like

myFunc({ some JSON data})

when it asks for http://.../myResource.js?callback=myFunc...
[Read More]

Friday Feb 22, 2008

Since today a better support for JSON data generated out of JAXB beans is available in Jersey.

The main improvement is a simpler default JSON data format.
Badgerfish convention was replaced by a new one, a slightly simplified so called mapped convention from Jettison. JSON convention used for particular beans is now also fully configurable by the end user.
I will show how the configuration could be done below.

The main differences between available formats could be described using the following example.
Lets start with a very simple JAXB bean. You can see the source code, XML output and then
all formats you can get from Jersey resource:

Friday Aug 31, 2007

To help people understand how things work it is useful to provide examples.
The blog entry contains a link to an example providing JSON data out from a JAXB object. It is build upon Jersey of course...