杰克逊用 getter 导出 属性 只给出 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

Jackson derived property with getter only gives com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

我的 class 看起来像这样

class Foo {
     int x;

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

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

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

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

我尝试使用 @JsonGetter 进行注释,但没有成功。

似乎与 Jackson 一起工作的唯一方法是创建一个什么都不做的 setter 并用 @JsonIgnore 注释它。

使用@JsonIgnoreProperties注释:

@JsonIgnoreProperties(ignoreUnknown = true)
class Foo

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