Reputation: 343
I have a String attribute "version" in java class. I am using Jackson object mapper to serialize Java Class Object to JSON.
I am serializing same class for two use cases, 1) to store JSON value to key-value db store, and 2) to send response back to API call.
For first use case I need "version" attribute in serialized JSON and for second use case I don't need it to send back to user.
Using @JsonIgnore to version will not get me that attribute in both use cases.
I searched enough to see that is there any configuration I can set on Jackson mapper to ignore @JsonIgnore in some cases? No luck so far! Thanks for suggestions or workarounds in advance.
Upvotes: 1
Views: 2253
Reputation: 3898
If you need to have multiple representations you can use @JsonView
.
public class Views {
public static class Public {
}
public static class Private {
}
}
public class User {
public int id;
@JsonView(Views.Public.class)
public String name;
}
https://www.baeldung.com/jackson-json-view-annotation
Upvotes: 1
Reputation: 40038
Do it in simple way, while in the second case set version
to null
and add this property on class level or property level
@JsonInclude(Include.NON_NULL)
Upvotes: 1