杰克逊(Jackson)的带有getter的派生属性仅给出com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException [英] Jackson derived property with getter only gives com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

查看:74
本文介绍了杰克逊(Jackson)的带有getter的派生属性仅给出com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的课看起来像这样

class Foo {
     int x;

     public void setX(int x){
        this.x=x;
     }

     public int getX(){
       return x;
     }
     public int getDoubleX(){
         return x*2;
     }
}

使用Jackson将类序列化为JSON时,出现错误:

When serializing the class to JSON using Jackson I get an error:

**JSON parse error: Unrecognized field "doubleX"**

我尝试使用@JsonGetter进行注释,但这没有用.

I tried annotating with @JsonGetter but that did not work.

Jackson唯一可行的方法是创建一个不执行任何操作的设置器,并使用@JsonIgnore对其进行批注.

The only thing that seems to work with Jackson is to create a setter that does nothing and annotate it with @JsonIgnore.

推荐答案

使用@JsonIgnoreProperties批注:

@JsonIgnoreProperties(ignoreUnknown = true)
class Foo

它应该允许序列化所有getters并在反序列化期间跳过未知的内容.

It should allow to serialise all getters and skip unknown during deserialisation.

这篇关于杰克逊(Jackson)的带有getter的派生属性仅给出com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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