JAX-RS or THE way to do REST in Domino Part 3.5

Soooo, I know I said the last segment was my last on this, but I thought I could share one other really cool thing about using JAX-RS.  Using JAX-RS allows you as a developer to avoid not only parsing JSON but also having intermediary objects like JsonJavaObject from IBM Commons.  It does this by allowing you to add in a mapper for both XML and JSON.  Which means you can have a model object that you develop and it will know how to translate it to and from JSON.

The basic tip is that you have to add one more method to your Wink Application Class, getSingleton.  This method basically lets you setup the mapper and add it to the framework.

The code to add this is as follows:


@Override
public Set<Object> getSingletons() {
Set<Object> s = new HashSet<Object>();
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary,     secondary);
mapper.getDeserializationConfig().setAnnotationIntrospector(pair);
mapper.getSerializationConfig().setAnnotationIntrospector(pair);
JacksonJaxbJsonProvider jaxbProvider = new   JacksonJaxbJsonProvider();
jaxbProvider.setMapper(mapper);
s.add(jaxbProvider);
return s;

}

For this code to work, you have to have in your classpath the jackson jar file.

Once you have added the jar file to your WebContent\Web-INF\lib\ folder

jackson

added it to the libaries tab in your Build Configuration

jackson3

you also need to add it to the Classpath Section in the plugin.xml under the runtime tab.

jackson2

Once you have that added to your class, now you can create Java classes and the framework will let you POST something like this:


{"fname":"Joe","lname":"User","email":"test@test.com","phone":"123.456.7841","birthdate":54544444}

at this method


@Path("/toby")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MyBean postMessage(MyBean mybean) {
mybean.setFname("Toby");
return mybean;
}

The method gets the data and converts it to a MyBean object automatically then I change it and send it back to the client and it comes down as JSON all without me adding anything to handle the JSON, except for the annotations.

It’s pretty magical really.

Hope you get some mileage out of this info.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s