[英]Jackson - Custom Deserializer doesn't read all the fields in JSON
I am receiving from the API this JSON:我从 API 收到这个 JSON:
"link": [],
"firstRecord": 1,
"item": [
{
"Customer": {
"id": "1058207",
"firstName": "foo",
"lastName": "foo2",
"nestedObj1": {
"id": "40008"
},
"nestedObj2": {
"link": [],
"linkfoo": "lala",
"item": [
{
"id": "266614",
"label": "NESTED_OBJ_2"
}
]
}
]
}
My Deserializer function我的解串器功能
@Override
public CustomerView deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
//tried this too
TreeNode treeNode = p.getCodec().readTree(p);
// also this
JsonNode node = p.getCodec().readTree(p);
JsonNode simpleNode = new ObjectMapper().readTree(p);
// use for each field that is not String
ObjectMapper mapper = new ObjectMapper();
Customer customer = new Customer();
customer.setFirstName(simpleNode.get("Customer").get("firstName").textValue());
NestedObj2[] arrayObj2 = mapper.readValue(
simpleNode.get("Customer").get("nestedObj2").get("item").toString(),
NestedObj2[].class);
customer.setArrayObj2(arrayObj2);
}
Class NestedObj2 has all the fields as in the JSON, "item" array is separate object as a field.类 NestedObj2 具有 JSON 中的所有字段,“item”数组是作为字段的单独对象。
The problem is, that neither JsonNode nor TreeNode, doesn't see field "nestedObj2", but the rest of the fields are inside them while deserializing -> checked while debugging.问题是,无论是 JsonNode 还是 TreeNode,都看不到字段“nestedObj2”,但在反序列化时,其余字段都在其中 -> 在调试时检查。
Do I miss something in configuration, or should I use other object to deserialize?我是否错过了配置中的某些内容,还是应该使用其他对象来反序列化?
Thanks!谢谢!
EDIT编辑
In the end I've used DTO as @Mehrdad HosseinNejad advised.最后,我按照@Mehrdad HosseinNejad 的建议使用了 DTO。 As I'm receiving this JSON by
RestTemplate.exchange()
, I had to configure RestTemplate
with MappingJacksonHttpMessageConverter
like here https://stackoverflow.com/a/9381832/12677470当我通过
RestTemplate.exchange()
收到这个 JSON 时,我必须像这里一样使用MappingJacksonHttpMessageConverter
配置RestTemplate
https://stackoverflow.com/a/9381832/12677470
Use of DTO classes may be a better idea, there is no need to create custom deserializer使用 DTO 类可能是一个更好的主意,无需创建自定义反序列化器
I coded an example nested DTO as follows我编写了一个示例嵌套 DTO,如下所示
Create DTO class for Customer
为客户创建 DTO 类
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {
private Long id;
private String firstName;
private String lastName;
private NestedObj1 nestedObj1;
private NestedObj2 nestedObj2;
//getter and setter
}
Create DTO class for NestedObj1
为 NestedObj1 创建 DTO 类
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NestedObj1 {
private Long id;
//getter and setter
}
Create DTO class for NestedObj2
为 NestedObj2 创建 DTO 类
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NestedObj2 {
private List<String> link;
private String linkFoo;
private List<Item> item;
//getter and setter
}
Create DTO class for Item
为 Item 创建 DTO 类
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item {
private Long id;
private String label;
//getter and setter
}
After creating these DTO's you can simply using ObjectMapper class convert your JSON to Java Object
创建这些 DTO 后,您可以简单地使用 ObjectMapper 类将您的 JSON 转换为 Java 对象
Customer customer= new ObjectMapper().readValue(jsonFile, Customer.class);
For more options like ignore some property,... you can use the below link:有关忽略某些属性等更多选项,...您可以使用以下链接:
Jackson Annotation Examples Jackson 注释示例
More information in Deserialization:反序列化中的更多信息:
Getting Started with Deserialization in Jackson Jackson 中的反序列化入门
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.