我需要在应用程序中有两个不同的ObjectMapper
。
Pojo我正在与:
public class Student {
private String name;
private Integer age;
@HideThisField
private String grade;
// getters & setters..
}
一种是基于ObjectMapper
的开箱即用配置,如下所示:
@Bean("objectMapper")
public ObjectMapper getRegularObjectMapper() {
//With some configurations
return new ObjectMapper();
}
我需要另一个ObjectMapper
,它在序列化时会根据字段上的注解忽略所有对象的一些字段。
@Bean("customObjectMapper")
public ObjectMapper getCustomObjectMapper() {
// This is where i want to ignore the fields with @HideThisField
return new ObjectMapper();
}
两个Map器的输出:objectMapper.writeValuesAsString(someStudent)
打印:
{【姓名】:【学生1】,年龄:10、“等级”:A +customObjectMapper.writeValuesAsString(someStudent)
打印:
{【姓名】:【学生1】,年龄:10个月}
1条答案
按热度按时间jtoj6r0c1#
JacksonAnnotationIntrospector
处理标准的Jackson
注解。通过覆盖hasIgnoreMarker
方法,您可以使其根据您自己的注解工作。控制台输出为:
getCustomObjectMapper()
不会跳过JsonIgnore
注解,因为您覆盖了标准,如果需要,您需要将其添加到if块中。*