Jackson - 自定义解串器未读取 JSON 中的所有字段

Jackson - Custom Deserializer doesn't read all the fields in 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"
                            }
                           ]
                  }
              ]
           }

我的解串器函数

    @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 具有 JSON 中的所有字段,"item" 数组是单独的对象作为字段。

问题是,JsonNode 和 TreeNode 都没有看到字段 "nestedObj2",但其余字段在反序列化时都在其中 -> 检查在调试时。

我是否遗漏了配置中的某些内容,或者我应该使用其他对象进行反序列化?

谢谢!

编辑

最后我按照@Mehrdad HosseinNejad 的建议使用了 DTO。 当我收到 RestTemplate.exchange() 的 JSON 时,我必须像这里

一样用 MappingJacksonHttpMessageConverter 配置 RestTemplate

使用 DTO 类 可能是更好的主意,无需创建自定义解串器

我编写了一个示例嵌套 DTO,如下所示

Create DTO class for Customer

@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

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NestedObj1 {

private Long id;

//getter and setter

}

Create DTO class for NestedObj2

@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

@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

Customer customer= new ObjectMapper().readValue(jsonFile, Customer.class);

有关忽略某些 属性 等更多选项,...您可以使用以下 link:

Jackson Annotation Examples

反序列化中的更多信息:

Getting Started with Deserialization in Jackson