我正面临一个问题。 Jackson映射缺少映射POJO类的一个键值。
使用这种行家依赖-
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
这是代码-
try {
ObjectMapper mapper = new ObjectMapper();
allGroupResponse = mapper.readValue(record, AllGroupResponse.class);
} catch (Exception e) {
e.printStackTrace();
}
POJO类
public class AllGroupResponse {
private String status;
private String data;
private String message;
private String tag;
private List<GroupTable> groupListDTO;
}
public class GroupTable {
private String id;
private String groupId;
private String groupName;
private String address;
private String groupLimit;
private String groupPairingCode;
private String zoneId;
private String siteContactNumber;
private String programId;
private String projectId;
private String createdBy;
private String updatedBy;
private String partyProfileId;
private String groupType;
private Date createdDate;
private Date updatedDate;
private Date groupPairingValidity;
private String groupManagerName;
private String groupManagerEmail;
private List<String> userEmailIds;
private String description;
private String status;
private String city;
private String state;
private String parentReference;
}
这是JSON字符串-
{
"data": null,
"groupListDTO": [
{
"groupType": "PARENT",
"city": null,
"groupId": "demo site_39329fbf-97ce-4474-bfab-db0f00ffa4b9",
"description": null,
"updatedDate": 1562310647155,
"parentReference": "null",
"zoneId": "demotest-zone_13f74715-fad9-4517-9cbd-bd47fa7c4df7",
"id": "0cab0526-ce7c-4cd8-80cd-77e1e7e52b29",
"state": null,
"groupLimit": null,
"address": "Mu",
"updatedBy": "nishant@gmail.com",
"groupManagerEmail": null,
"groupPairingCode": null,
"groupManagerName": null,
"groupName": "Demo Site",
"createdDate": 1557490542612,
"createdBy": null,
"groupPairingValidity": null,
"partyProfileId": null,
"siteContactNumber": "",
"projectId": "ab4dafed-7fd9-46c0-ab9a-b28d756c4f11",
"programId": null,
"userEmailIds": ["jl@gmail.com"],
"status": "UPDATED"
}
],
"tag": "GROUP_LIST_FOUND",
"message": "Get all the group list for user.",
"status": "SUCCESS"
}
映射到POJO类之后。打印到字符串-
AllGroupResponse{status='SUCCESS', data='null', message='Get all the group list for user.', tag='GROUP_LIST_FOUND', groupListDTO=[GroupTable{id='0cab0526-ce7c-4cd8-80cd-77e1e7e52b29', groupId='demo site_39329fbf-97ce-4474-bfab-db0f00ffa4b9', groupName='Demo Site', address='Mu', groupLimit='null', groupPairingCode='null', zoneId='demotest-zone_13f74715-fad9-4517-9cbd-bd47fa7c4df7', siteContactNumber='', programId='null', projectId='ab4dafed-7fd9-46c0-ab9a-b28d756c4f11', createdBy='null', updatedBy='nishant@gmail.com', partyProfileId='null', groupType='PARENT', createdDate=Fri May 10 17:45:42 IST 2019, updatedDate=Fri Jul 05 12:40:47 IST 2019, groupPairingValidity=null, groupManagerName='null', groupManagerEmail='null', userEmailIds=[jl@gmail.com], description='null', status='UPDATED', city='null', state='null'}]}
在这里,我发现密钥"parentReference": "null"
丢失了。我知道null值是字符串格式。但是它应该映射,因为它被认为是String。
我不明白为什么会这样。没有异常发生。请让我知道解决方法。
答案 0 :(得分:1)
忘记在toString()方法中添加更新的参数- 谢谢@ alexey28
@Override
public String toString() {
return "GroupTable{" +
"id='" + id + '\'' +
", groupId='" + groupId + '\'' +
", groupName='" + groupName + '\'' +
", address='" + address + '\'' +
", groupLimit='" + groupLimit + '\'' +
", groupPairingCode='" + groupPairingCode + '\'' +
", zoneId='" + zoneId + '\'' +
", siteContactNumber='" + siteContactNumber + '\'' +
", programId='" + programId + '\'' +
", projectId='" + projectId + '\'' +
", createdBy='" + createdBy + '\'' +
", updatedBy='" + updatedBy + '\'' +
", partyProfileId='" + partyProfileId + '\'' +
", groupType='" + groupType + '\'' +
", createdDate=" + createdDate +
", updatedDate=" + updatedDate +
", groupPairingValidity=" + groupPairingValidity +
", groupManagerName='" + groupManagerName + '\'' +
", groupManagerEmail='" + groupManagerEmail + '\'' +
", userEmailIds=" + userEmailIds +
", description='" + description + '\'' +
", status='" + status + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", parentReference='" + parentReference + '\'' +
'}';
}