users@jersey.java.net

[Jersey] Re: Controlling JSON to POJO mapping

From: Carsten Byrman <carsten_at_byrman.demon.nl>
Date: Thu, 20 Sep 2012 09:22:10 +0200

Thanks for the example code, Mike.

Carsten

On 09/19/2012 02:04 PM, Mike Summers wrote:
> If by "just annotations" you mean something that requires no code,
> then yes... you can't do this with "just annotations".
>
> In JAXB this does what you want:
> @XmlAccessorType(XmlAccessType.NONE)
> @XmlRootElement
> public class Persons {
> private List<Person> persons = new ArrayList<Person>();
>
> @XmlElement(name = "first_names")
> public List<String> getFirstNames() {
> List<String> firstNames = new ArrayList<String>(persons.size());
> for (Person person : persons)
> firstNames.add(person.getFirstName());
> return firstNames;
> }
>
> @XmlElement(name = "last_names")
> public List<String> getLastNames() {
> List<String> lastNames = new ArrayList<String>(persons.size());
> for (Person person : persons)
> lastNames.add(person.getLastName());
> return lastNames;
> }
>
> public Persons testData() {
> persons.add(new Person("Tom", "Skerritt"));
> persons.add(new Person("Sigourney", "Weaver"));
> persons.add(new Person("Veronica", "Cartwright"));
> persons.add(new Person("Harry Dean", "Stanton"));
> persons.add(new Person("John", "Hurt"));
> persons.add(new Person("Ian", "Holm"));
> persons.add(new Person("Yaphet", "Kotto "));
> persons.add(new Person("Bolaji", "Badejo"));
> persons.add(new Person("Helen", "Horton"));
> return this;
> }
> }
> public class Person {
> private String firstName;
> private String lastName;
>
> public Person(String firstName, String lastName) {
> this.firstName = firstName;
> this.lastName = lastName;
> }
>
> public String getFirstName() {
> return firstName;
> }
>
> public String getLastName() {
> return lastName;
> }
>
> public void setFirstName(String firstName) {
> this.firstName = firstName;
> }
>
> public void setLastName(String lastName) {
> this.lastName = lastName;
> }
> }
>
> Sep 19, 2012 6:57:50 AM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 1 * Client in-bound response
> 1 < 200
> 1 < Date: Wed, 19 Sep 2012 11:57:50 GMT
> 1 < Content-Length: 233
> 1 < server: grizzly/1.9.45
> 1 < Content-Type: application/json
> 1 <
> {
> "first_names" : [ "Tom", "Sigourney", "Veronica", "Harry Dean",
> "John", "Ian", "Yaphet", "Bolaji", "Helen" ],
> "last_names" : [ "Skerritt", "Weaver", "Cartwright", "Stanton",
> "Hurt", "Holm", "Kotto ", "Badejo", "Horton" ]
> }
>
> The setters on Persons are a bit more complex, I'll leave that to you :-)
>
> On Tue, Sep 18, 2012 at 4:58 PM, Carsten Byrman
> <carsten_at_byrman.demon.nl <mailto:carsten_at_byrman.demon.nl>> wrote:
>
> 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 <mailto: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
>
>