RSS Feed!

Recent Posts

Recent Comments

Jersey 1.0.2 JSON and JAXB

In a previous post I explained how to unit test JAX-RS. Let’s now have a look at a more complex example: implementing RESTful webservices using Jersey 1.0.2.

To get RESTful web services (JAX-RS) that produce JSON output using Jersey you have to get the following libraries:
1. Jersey 1.0.2 (see the dependencies here for a web application. Make sure you get: jersey-server.jarjersey-core.jarjsr311-api.jarasm.jar)
2. JAXB 2.1 (jaxb-impl.jarjaxb-api.jar) If you run into problems later just check that you are running jaxb 2.1.10. The 2.1.10 jaxb-impl.jar provides support for the natural jsonconfiguration.
3. JSON-lib 2.2.3 (json-lib-2.2.3-jdk15.jar) You will use this later to construct a MessageBodyWriter Provider that outputs JSON content…
Let’s start setting up the whole lot.
WEB.XML
In your web application’s web.xml make sure you’ve hooked up Jersey:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<servlet>
  <servlet-name>My Jersey Web Application</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
    <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
  </init-param>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>net.tmro.demo.json;your.packages;separated.by.semicolons</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>My Jersey Web Application</servlet-name>
  <url-pattern>/webappjson/*</url-pattern>
</servlet-mapping>
1. The PackagesResourceConfig param enables your to do automatic discovery of your relevant classes as long as you provide the packages location.
2. The url pattern is the endpoint where your resources will be made available.
The RESTful resource
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package net.tmro.demo.json;
 
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
 
@Path(/myresource”)
public class MyResource {
 
	public MyResource() {
	}
 
	@GET
	@Produces(value=MediaType.APPLICATION_JSON)
	@Path(/getsomething”)
	public MyComplexObject getSomething(@QueryParam(“idOfSomething”) String id) {
		//your complex object can expose primitive data types, objects as well as collections.
		//you are not required to do anything fancy as long as you implement
		return new MyComplexObject(id);
	}
}
To get something your endpoint will now look like: http://yourserver:port/appcontext/webappjson/myresource/getsomething?ifOfSomething=id The parts in bold represent what you’ve defined so far.
The @Get annotation specifies what type of requests are handled by this method.
The @Path allows you to discriminate between several methods that you might have in this resource class.
If in your @Produces you were to specify a MediaType.APPLICATION_XML you’d get a neat xml response when pointing your browser at the endpoint above. But what we actually want is a JSON message. So let’s continue.
JSON MessageBodyWriter
The xml returned by our resource needs to be converted into JSON format. I used the json-lib that I mentioned above. Here’s the code for it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package net.tmro.demo.json;
 
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
 
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
 
import com.sun.jersey.spi.resource.Singleton;
 
import net.sf.json.JSONSerializer;
 
@Provider
@Singleton
public class MyEntityProvider implements MessageBodyWriter<object> {
	//the json serializer used to write out objects to the output stream
	private final JSONSerializer serializer = new JSONSerializer();
 
	public long getSize(final Object t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
		return -1;
	}
 
	public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
		return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
	}
 
	public void writeTo(final Object t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<string, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
		entityStream.write(serializer.toJSON(t).toString().getBytes());
	}
}
Not much going on here: we implement the MessageBodyWriter interface and we only use this entity provider to handle JSON mime types.
Note that the class is annotated @Provider so make sure that you have specified this package in your web.xml deployment descriptor.
Returned objects
The only thing that is still missing is the actual object that is returned. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package net.tmro.demo.json;
 
@XmlRootElement
public class MyComplexObject extends AnotherObject {
 
	//fields: primitive data types, objects and collections. everything gets serialized unless
	//marked as transient
 
	public MyComplexObject(String id){
	[..]
	}
 
	//getters and setters
}
The only thing that I have done really is to annotate it with @XmlRootElement. If you have properties that you do not want to return simply annotate them with @XmlTransient.
Now build and deploy your application and keep an eye on the log. You should see:
- Scanning for root resource and provider classes in the packages:
and then a list of your packages defined in the web.xml
Root resource classes found:
followed by your Resource class.
Provider classes found:
followed by the entity provider that you have implemented.
To test just point your browser to the url I described above and your browser should prompt you to save some streamed data. Open it with a text viewer and you should be able to see your JSON response.
Cheers…
Before I conclude: if you’ve tried to get things going and ended up with something like this:
com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java type, class [..], and MIME media type, application/json, was not found
com.sun.jersey.server.impl.application.WebApplicationImpl onException
SEVERE: Internal server error
javax.ws.rs.WebApplicationException
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:241)
you might want to check that your jaxb and json lib versions are OK and that the MessageBodyWriter implementation is picked up when you start your web app…
I would be interested to find out if you managed to find a way to NOT implement the MessageBodyWriter but still get JSON output.

Tags: , , , , , ,

By Nick | 29. May 2009 | Java, REST, Tutorial | 6 Comments »

6 Comments

  1. Sergio says:

    If you use jettison you don’t need not implement the MessageBodyWriter. The entity providers that come with jersey already support Jettison.
    Besos

  2. Nick says:

    Sadly, I have jettison 1.0.1 in my path (deployed with the webapp) and it doesn’t get picked up. Did you have to do any configuration (e.g. web.xml init param)?

  3. Nick says:

    UPDATE: upgrading jettison to 1.1 seemed to do the trick.

  4. JP says:

    Thanks for taking the time to write this. However, it's nto clear to me how I can generate XML instead of JSON? Furthermore, I'm using Google App Engine… so the XML bindings don't work =(

  5. Nick says:

    JP, to produce XML use
    @Produces(value=MediaType.APPLICATION_XML)

    Have a look at the MediaType api to see what other options you have.

    Sorry, I am not familiar with the restrictions brought about by the Google App Engine.

Leave a Reply