java - 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;
}
解决方案
正如这里用一个例子解释的:
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
推荐阅读
- three.js - 三.js 为拉伸形状上的BasicMeshMaterial 初始化颜色后,渲染到画布上的颜色不同
- javascript - 在 Rails 中使用 ES6 语法和 Javascript 缩小的直接、受支持、稳定的方式是什么?
- julia - 如何在给定分隔符的情况下将 DataFrame 列分成两个?
- javascript - 在计时器结束时显示一段时间的消息
- java - 如何检测 equals() 和 hashCode() 是否与类的字段不同步?
- java - 如何使用 JOOQ 在 FROM 子查询中添加 jsonb 函数
- react-native - 包含数千个文件的 DownloadFile 阻塞了应用程序界面
- javascript - 你如何将一个动作与一个 DOM 事件一起分派?
- light-inject - 如何将实例注入当前范围?
- c# - 如何仅通过 c# 从动态 SSL 页面获取文本块