I have a need to have two different ObjectMapper
in the application.
Pojo I am working with:
public class Student {
private String name;
private Integer age;
@HideThisField
private String grade;
// getters & setters..
}
One is the out of the box configuration based ObjectMapper
as below:
@Bean("objectMapper")
public ObjectMapper getRegularObjectMapper() {
//With some configurations
return new ObjectMapper();
}
I need another ObjectMapper
that while serializing ignores a few fields for all objects based on an annotation on a field.
@Bean("customObjectMapper")
public ObjectMapper getCustomObjectMapper() {
// This is where i want to ignore the fields with @HideThisField
return new ObjectMapper();
}
Output of the two mappers:
objectMapper.writeValuesAsString(someStudent)
prints:
{"name": ""student1", age: 10, "grade": "A+"}
customObjectMapper.writeValuesAsString(someStudent)
prints:
{"name": ""student1", age: 10}