users@jersey.java.net

[Jersey] Re: Jersey with Jackson for JSON

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Sat, 18 Jun 2011 10:01:05 -0700

On Sat, Jun 18, 2011 at 3:53 AM, Sven Strittmatter
<weltraumschaf_at_googlemail.com> wrote:
> Hi Jakub,
>
> Am 17.06.11 11:36, schrieb Jakub Podlesak:
> Thanx for your help :)
>
>> For more recent information on JSON processing configuration
>> in Jersey, i would suggest you see [2], try example [3] out
>
> I studied all that and it gave me some insight. But it is about 'generating'
> JSON from my Jersy resource for the client.
>
> I want to make a HTTP request from inside my Jersey app to an other
> REST/JSON API and work with the responded JSON. Imagine I wanna do a Twitter
> client with Jersey or such.
>
> What I see: I have to do some mapping with Beans and provide a
> JAXBContextResolver for that. But I didn't get it 100%, yet.
> I'm trying to understand your code from [1].

If you are calling another service, you don't necessarily need any
support from Jersey.
You just need an ObjectMapper (org.codehaus.jackson.map.ObjectMapper)
for data binding, and ability to make a HTTP request
(URL.openConnection for JDK default, or async-http-client or apache
httpclient 4.x).
Jersey may help with dependency injection; so perhaps you can add
@Inject to ObjectMapper field (instead of creating a static singleton
yourself).

If you do want more Jersey goodness, you can check out jersey-client
side, which can simplify things. But calling a REST/JSON web service
really is rather simple; the only complex part is dealing with
(transient) failures, like timeouts, retrying.

As to JSON, OOP; just to be sure, Jackson allows you not only to bind
data to "real" objects, but you can also just use basic Maps:

  Map<String,Object> jsonMap = objectMapper.readValue(inputStream, Map.class);

or "JSON tree", expressed as JsonNodes:

  JsonNode rootNode = objectMapper.readTree(inputTree);

which may be comfortable coming from more dynamic (scripting) languages.

-+ Tatu +-