我正在使用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 :
Without
@JsonUnwrapped
, the JSON is :With
@JsonUnwrapped
, the JSON is :So
@JsonUnwrapped
will flatten the properties andevents
won't exist anymore :Try to remove
@JsonUnwrapped