users@jersey.java.net

[Jersey] Re: Controlling JSON to POJO mapping

From: Mike Summers <msummers57_at_gmail.com>
Date: Tue, 18 Sep 2012 07:02:31 -0500

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
>