首页 > 解决方案 > Java Spring使用RestTemplate反序列化嵌套对象

问题描述

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

我这样做对吗?我应该返回一个字符串响应实体然后转换吗?

{
  "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;

}

标签: javajsonspringserializationjackson

解决方案


正如这里用一个例子解释的:

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

没有@JsonUnwrapped,JSON 是:

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

随着@JsonUnwrapped,JSON 是:

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

因此@JsonUnwrapped将展平属性并且events将不再存在:

{
    "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",
    ...
}

尝试删除@JsonUnwrapped


推荐阅读