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

杀马特。学长 韩版系。学妹 提交于 2019-12-25 01:36:25

问题


My class looks like this

class Foo {
     int x;

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

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

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

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

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

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


回答1:


Use @JsonIgnoreProperties annotation:

@JsonIgnoreProperties(ignoreUnknown = true)
class Foo

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



来源:https://stackoverflow.com/questions/55164244/jackson-derived-property-with-getter-only-gives-com-fasterxml-jackson-databind-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!