gpt4 book ai didi

spring-boot - Jackson - 通过 Jackson2ObjectMapperBuilderCustomizer 配置集合覆盖

转载 作者:行者123 更新时间:2023-12-04 17:54:57 24 4
gpt4 key购买 nike

我在我的 Spring Boot 配置中通过构建一个新的映射器来自定义我的 Jackson 对象映射器中的集合处理

@Configuration
public class Config {

@Autowired(required = true)
public void objectMapper(ObjectMapper mapper) {

mapper.configOverride(Collection.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));
mapper.configOverride(List.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));
mapper.configOverride(Map.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null));

}

虽然这可行,但我知道更优雅的方法是使用 Jackson2ObjectMapperBuilderCustomizer :

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizeJackson2ObjectMapper() {
return new Jackson2ObjectMapperBuilderCustomizer() {

@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder
.indentOutput(true)
.someOtherMethod(...)

}
};
}

如何通过 Jackson2ObjectMapperBuilder 实现上面的 ObjectMapper 集合调整?

最佳答案

您可以使用本地定义的简单 Module,如 this other use case . SetupContext 也有一个 configOverride() 方法,就像 ObjectMapper 本身一样。

关于spring-boot - Jackson - 通过 Jackson2ObjectMapperBuilderCustomizer 配置集合覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com