Java Spring使用RestTemplate反序列化嵌套对象

我正在使用Java Spring Boot restTemplate,并且尝试将以下JSON反序列化为它们对应的对象。但是,它返回null。

我这样做正确吗?我应该返回一个String响应实体然后进行转换吗?

{
  "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"
    }
 ]
}

这是我的电话:

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

return response.getBody().getEvents();

这是我的WaEvents类:

@Data
public class WaEvents implements Serializable {

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

这是WaEvent类

@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;

}
评论
  • 莫~大宝
    莫~大宝 回复

    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