Jackson2ObjectMapperBuilder 启用字段可见性任何 [英] Jackson2ObjectMapperBuilder enable field visibility ANY
问题描述
我正在使用 spring-boot 并且想要自定义创建的 ObjectMapper.
I'm using spring-boot and want to customize the ObjectMapper created.
我想要做的是能够序列化没有 getter 或 setter 的对象.在这之前,可以通过将 JsonAutoDetect.Visibility.ANY 放在 ObjectMapper 上来完成.
What I want to do is be able to serialize objects that do not have a getter or setters. Before this could be done by putting JsonAutoDetect.Visibility.ANY on the ObjectMapper.
但是如何使用我当前公开的 Jackson2ObjectMapperBuilder bean 启用此功能?
But how can I enable this feature using the Jackson2ObjectMapperBuilder bean I'm currently exposing ?
推荐答案
您可以使用覆盖 configure(ObjectMapper)
方法的 Jackson2ObjectMapperBuilder
子类:
You can use a Jackson2ObjectMapperBuilder
subclass that overrides the configure(ObjectMapper)
method:
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
return new Jackson2ObjectMapperBuilder() {
@Override
public void configure(ObjectMapper objectMapper) {
super.configure(objectMapper);
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
}
};
}
这篇关于Jackson2ObjectMapperBuilder 启用字段可见性任何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!