问题描述
我正在使用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 ?
推荐答案
您可以使用 Jackson2ObjectMapperBuilder
子类覆盖 configure(ObjectMapper)
方法:
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启用字段可见性ANY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!