如何在Spring Boot 1.4中自定义Jackson
我无法找到如何使用Jackson2ObjectMapperBuilderCustomizer的示例.java在spring boot 1.4中自定义Jackson的功能。
用于在靴子中自定义杰克逊的 doco 1.4 - https://docs.spring.io/spring-boot/docs/1.4.x/reference/htmlsingle/#howto-customize-the-jackson-objectmapper
我的配置有效,尽管我不确定这是否是使用Jackson2ObjectMapperBuilderCustomizer自定义对象映射器的正确方法.java
@Configuration
public class JacksonAutoConfiguration {
@Autowired
private Environment env;
@Bean
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
Jackson2ObjectMapperBuilder builder = configureObjectMapper();
customize(builder, customizers);
return builder;
}
private void customize(Jackson2ObjectMapperBuilder builder,
List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
customizer.customize(builder);
}
}
private Jackson2ObjectMapperBuilder configureObjectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
List<String> activeProfiles = asList(env.getActiveProfiles());
if (activeProfiles.contains(SPRING_PROFILE_DEVELOPMENT)) {
builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
}
return builder;
}
}
为了提供一些上下文,这个类位于我自己的REST服务的弹簧启动器项目中,该项目只是自动配置许多东西,如ControrAdvice和一些琐碎的功能,如上面。
所以我的目标是扩展 Jackson 配置,而不是覆盖 boot 或其他软件包提供的任何配置。