我正在使用spring-boot,并且想要自定义创建的ObjectMapper。
我想做的是能够序列化没有getter或setter的对象。在此之前,可以将JsonAutoDetect.Visibility.ANY放在ObjectMapper上。
但是,如何使用我当前公开的Jackson2ObjectMapperBuilder bean启用此功能?
您可以使用Jackson2ObjectMapperBuilder覆盖该configure(ObjectMapper)方法的子类:
Jackson2ObjectMapperBuilder
configure(ObjectMapper)
@Bean public Jackson2ObjectMapperBuilder objectMapperBuilder() { return new Jackson2ObjectMapperBuilder() { @Override public void configure(ObjectMapper objectMapper) { super.configure(objectMapper); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); } }; }