users@jersey.java.net

[Jersey] Re: Controlling JSON to POJO mapping

From: Mike Summers <msummers57_at_gmail.com>
Date: Tue, 18 Sep 2012 09:58:05 -0500

I personally like JAXB due to the control I get in a standardized way, but
I also have to support multiple formats (JSON, XML, Lua, ...) and
formatting requirements (what the result looks like) so I don't have a
choice

Without knowing a lot more about your app I don't want to venture an
opinion about POJO vs JAXB, that is a "big picture" sort of question. And I
suspect there are as many opinions on this as there are mailing list
subscribers :-)

Just to add further confusion, there are also Jackson annotations to
consider.


On Tue, Sep 18, 2012 at 7: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)?
>
> Carsten
>
>
> On 09/18/2012 02:02 PM, Mike Summers wrote:
>
> This is really a JAXB question, but here goes:
> public class Person{
> private String firstName;
> private String lastName;
> }
>
> public class Persons{
> private List<Person> person;
> }
>
> Should start you to where you want to go;
>
> On Tue, Sep 18, 2012 at 3:58 AM, <carsten_at_byrman.demon.nl> wrote:
>
>> I am using Jersey's POJO mapping feature to map JSON responses to Java
>> objects. This works, but I would like to have more control. For
>> example, consider the following JSON, which uses arrays to minimize the
>> payload:
>>
>> {
>> "first_names": [
>> "Ellen",
>> ...
>> ],
>> "last_names": [
>> "Ripley",
>> ...
>> ]
>> }
>>
>> This can easily be mapped to:
>>
>> public class Persons {
>>
>> @JsonProperty("first_names")
>> private List<String> firstNames;
>> @JsonProperty("last_names")
>> private List<String> firstNames;
>>
>> }
>>
>> However, I find this inconvenient and ugly. I would rather prefer
>> somehow a list of Person POJOs, each one having a firstName and
>> lastName as a String. Is this possible? Should I revert to low-level
>> JSON support or can this be achieved via the POJO mapping feature?
>>
>> I have read the Jersey User Guide, but still have no clue on how to do
>> this. I would be grateful if someone could point me in the right
>> direction.
>>
>> Carsten
>>
>
>
>