如何知道@JsonIgnoreProperties忽略了哪些所有属性(ignoreUnknown = true) [英] How to know what all properties are ignored by @JsonIgnoreProperties(ignoreUnknown=true)

查看:295
本文介绍了如何知道@JsonIgnoreProperties忽略了哪些所有属性(ignoreUnknown = true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要序列化-在我的代码中反序列化现有的Java POJO. POJO很大+在层次结构中几乎没有父类.该代码使用spring,因此内部使用Jackson. 我通过修复getter-setter名称(包括@JsonIgnore等)发现了一个问题,开始解决了一个问题,经过相当长的时间,我将其完全修复.

I need to serialize - deserialized an existing Java POJO in my code. The POJO is big + it has few parent classes in the hierarchy. The code is using spring and so Jackson internally. I started fixing one by one issue I found by fixing getter-setter name, including @JsonIgnore etc and after considerable time I fixed it completely.

但是我必须修复几个这样的类,因此对于下一个类,我只添加了: @JsonIgnoreProperties(ignoreUnknown=true)起作用了,但是在测试中我发现它忽略了一个不应忽略的属性.该属性就像

But I have to fix several such classes, so for the next class, I just added: @JsonIgnoreProperties(ignoreUnknown=true) which worked but during the testing I found it ignored a property it should not ignore. The property was like

@JsonIgnoreProperties(ignoreUnknown=true)
class MyClass {
   private String xyz;
   public String getXyzValue() {
     return this.xyz;
   }
   public void setXyz(String xyz) {
     this.xyz = xyz;
   }
}

所以基本上我必须在这里更正getter方法.

So basically I had to correct the getter method here.

问题:是否可以使用@JsonIgnoreProperties(ignoreUnknown=true),但列出所有被忽略的属性以进行进一步分析?

Question: Is there a way to use @JsonIgnoreProperties(ignoreUnknown=true) but list down all ignored properties for further analysis?

推荐答案

删除JsonIgnoreProperties批注并注册您自己的com.fasterxml.jackson.databind.deser.DeserializationProblemHandler问题处理程序.参见以下示例:

Remove JsonIgnoreProperties annotation and register your own com.fasterxml.jackson.databind.deser.DeserializationProblemHandler problem handler. See below example:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.IOException;

public class JsonApp {

    public static void main(String[] args) throws IOException {
        String json = "{\"xyz\":\"X\",\"a\":1,\"yxz\":2}";
        DeserializationProblemHandler handler = new DeserializationProblemHandler() {
            @Override
            public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
                System.out.println("Unknown property '" + propertyName + "' for " + beanOrClass.getClass());
                return true;
            }
        };
        JsonMapper mapper = JsonMapper.builder()
                .addHandler(handler)
                .build();

        mapper.readValue(json, MyClass.class);
    }
}

上面的代码显示:

Unknown property 'a' for class com.example.MyClass
Unknown property 'yxz' for class com.example.MyClass

注意

JsonMapper 2.10中引入了a>类.在此版本以下,您可以使用ObjectMapper构造函数.

Note

JsonMapper class is introduced in version 2.10. Below this version you can use ObjectMapper constructor.

这篇关于如何知道@JsonIgnoreProperties忽略了哪些所有属性(ignoreUnknown = true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆