简体   繁体   中英

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

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 .

Use @JsonIgnoreProperties annotation:

@JsonIgnoreProperties(ignoreUnknown = true)
class Foo

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM