1

I am using Java Spring boot restTemplate and I am trying to deserialize the below JSON into their corresponding objects. However it is returning null.

Am I doing this the right way? Should I return a String response Entity and then convert?

{
  "Events": [
    {
      "Id": 3584588,
      "Url": "https://api.wildapricot.org/v2/accounts/257051/Events/3584588",
      "EventType": "Regular",
      "StartDate": "2019-10-07T07:00:00-05:00",
      "EndDate": "2019-10-11T12:00:00-05:00",
      "Location": "Renaissance Montgomery Hotel & Spa",
      "RegistrationEnabled": false,
      "RegistrationsLimit": null,
      "PendingRegistrationsCount": 0,
      "ConfirmedRegistrationsCount": 0,
      "CheckedInAttendeesNumber": 0,
      "InviteeStat": {
        "NotResponded": 0,
        "NotAttended": 0,
        "Attended": 0,
        "MaybeAttended": 0
      },
      "Tags": [
        "event"
      ],
      "AccessLevel": "AdminOnly",
      "StartTimeSpecified": true,
      "EndTimeSpecified": true,
      "HasEnabledRegistrationTypes": false,
      "Name": "2020 Montgomery IT Summit"
    },
    {
      "Id": 3584591,
      "Url": "https://api.wildapricot.org/v2/accounts/257051/Events/3584591",
      "EventType": "Rsvp",
      "StartDate": "2019-10-03T00:00:00-05:00",
      "EndDate": "2019-10-31T00:00:00-05:00",
      "Location": "Here",
      "RegistrationEnabled": true,
      "RegistrationsLimit": null,
      "PendingRegistrationsCount": 0,
      "ConfirmedRegistrationsCount": 0,
      "CheckedInAttendeesNumber": 0,
      "InviteeStat": {
        "NotResponded": 0,
        "NotAttended": 0,
        "Attended": 0,
        "MaybeAttended": 0
      },
      "Tags": [
        "volunteer"
      ],
      "AccessLevel": "Public",
      "StartTimeSpecified": false,
      "EndTimeSpecified": false,
      "HasEnabledRegistrationTypes": true,
      "Name": "Volunteer Event"
    }
 ]
}

Here is my call:

ResponseEntity<WaEvents> response = restTemplate.exchange(uri,
                HttpMethod.GET,
                request,
                WaEvents.class
        );

return response.getBody().getEvents();

Here is my WaEvents Class:

@Data
public class WaEvents implements Serializable {

    @JsonUnwrapped
    @JsonProperty("Events")
    private List<WaEvent> events;
}

Here is the WaEvent Class

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class WaEvent {

    @JsonProperty("Id")
    public Integer id;

    @JsonProperty("Name")
    public String name;

    @JsonProperty("Location")
    public String location;

    @JsonProperty("StartDate")
    public LocalDate startDate;

    @JsonProperty("EndDate")
    public LocalDate endDate;

    @JsonProperty("IsEnabled")
    public Boolean isEnabled;

    @JsonProperty("Description")
    public String description;

    @JsonProperty("RegistrationLimit")
    public Integer RegistrationLimit;

}
5
  • Are you sure you are getting data from the API? Commented May 23, 2020 at 21:26
  • Yes. I replaced WaEvent with String and I can see the response body has the data. code ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, request, String.class ); Commented May 23, 2020 at 21:42
  • Then can you please post the full code of the WaEvent and WaEvents Commented May 23, 2020 at 22:04
  • That is the complete code. I am using lomback for the getters, setters and constructor if that is what you are looking for. Maybe I should try and do it manually? Commented May 23, 2020 at 22:23
  • I think so and add empty arguments constructor Commented May 23, 2020 at 22:37

1 Answer 1

3

As explained here with an example :

public class Parent {
    public int age;
    public Name name;
}
public class Name {
    public String first, last;
}

Without @JsonUnwrapped, the JSON is :

{
    "age" : 18,
    "name" : {
        "first" : "Joey",
        "last" : "Sixpack"
    }
}

With @JsonUnwrapped, the JSON is :

{
    "age" : 18,
    "first" : "Joey",
    "last" : "Sixpack"
}

So @JsonUnwrapped will flatten the properties and events won't exist anymore :

{
    "Id": 3584588,
    "Name": "2020 Montgomery IT Summit",
    "Location": "Renaissance Montgomery Hotel & Spa",
    "StartDate": "2019-10-07T07:00:00-05:00",
    "EndDate": "2019-10-11T12:00:00-05:00",
    ...
}

Try to remove @JsonUnwrapped

Sign up to request clarification or add additional context in comments.

1 Comment

I figured it out.... I was using the wrong Jackson package.... I was using org.codehaus.jackson versus com.fasterxml.jackson.core ...I think I would have still had the problem and your answer taught me something as well! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.