简体   繁体   中英

Jackson ObjectMapper jsonIgnore issue

I am beginner in Jackson. I am trying this -:

public class A {  
  boolean property1;
  String property2;
  // public getters and setters for both  
 }

public abstract class MixIn {
    @JsonIgnore
    boolean property1;
  }

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(A.class, MixIn.class);  

Now,

String result = mapper.writeValueAsString(new A())

gives me result like this - {"property2" : "value"} which is correct but if try to convert the result in the object again -:

mapper.readValue(result, A.class);

I am getting property1 back object A this -:

 A {property1 : false, property2 : value}

Why is objectMapper not ignoring the property again. Note - I tried directly putting @JsonIgnore on property1 and it worked fine but I have to use MixIn for this. I had tried putting JsonIgnore on getter and setters in MixIn too but that didn't work too.

Ok. I got it now. @JsonIgnore means that the property won't be written in the serialized string but if any string would be deserialized the same property would have defaults which in case of a boolean is always false which I realised when I tried with adding more properties so mix-in is actually working. It's just that I thought that in some magical way it would remove the property from the object itself. (I know it's silly.)

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