users@jersey.java.net

[Jersey] Re: Controlling JSON to POJO mapping

From: Carsten Byrman <carsten_at_byrman.demon.nl>
Date: Tue, 18 Sep 2012 23:58:25 +0200

On 09/18/2012 07:33 PM, Tatu Saloranta wrote:
> On Tue, Sep 18, 2012 at 5:46 AM, Carsten Byrman <carsten_at_byrman.demon.nl> wrote:
>> Thanks for your time, Mike.
>>
>> So 5.2 of the Jersey User Guide (JAXB Based JSON support) is the way to go -
>> even if the web service only returns JSON (no XML)?
> Example given did not actually use any of JAXB, not even annotations,
> so it's bit of a red herring.
> Structure given works as-is with Jackson-based POJO-mapping.
>
> -+ Tatu +-
Thanks for your help as well, Tatu.

I am still a bit confused. Let me try to summarize. The web service only
returns JSON. I do not have control over the JSON format. To minimize
the payload, it returns arrays of strings (first_names, last_names). I
am able to map this using Jersey's POJO mapping feature, but I do not
like the corresponding Java class. I would rather prefer a List<Person>.

Can this be achieved by just using annotations (either Jackson or JAXB)?
In your previous email, Tatu, you mention @JsonProperty("first_name"),
but that does not correspond to the actual JSON, which has only
"first_names" and "last_names" (plural):

{
     "first_names": [
         "Ellen",
         ...
     ],
     "last_names": [
         "Ripley",
         ...
     ]
}

I still don't get it how to map this to List<Person>, Person being:

public class Person{
     private String firstName;
     private String lastName;
}

I have the feeling that this cannot be achieved by annotations only -
but please, correct me if I'm wrong.

Carsten