com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 [英] com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

查看:1907
本文介绍了com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了反序列化问题:

i got a deserialization problem:

这是我的班级:

public class Response {

    private Object ResObj;
    private int ResInt;

    public Object getResObj() {
        return ResObj;
    }

    public int getResInt() {
        return ResInt;
    } 
} 

我要反序列化的JSON是:

the JSON i want to deserialize is:

{"ResObj":{"ClientNum":"12345","ServerNum":"78945","IdNum":"020252"},"ResInt":0}

我得到这个例外:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ResObj" , not marked as ignorable (0 known properties: ])
 at [Source: java.io.StringReader@1f758500; line: 1, column: 20] (through reference chain: ["ResObj"])

我不喜欢我想添加:

@JsonIgnoreProperties(ignoreUnknown = true)

因为我想获得ResObj ...

because I want to get the ResObj...

如果我添加注释,它会通过但是它将它设置为null ..我不想要。

if I add the annotation, it pass but it will set it as null .. which I don't want.

推荐答案

如果你不想在你的bean中设置setter并且只使用字段和getter,您可以使用ObjectMapper的可见性检查器来允许字段可见性。类似于以下内容

If you dont want to have setter in your bean and only use fields and getters, you can use visibility checker of ObjectMapper to allow field visibility. Something like following

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));

这篇关于com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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