首页 > 解决方案 > 使用 JsonFactory.Feature 设置在 Spring Boot 中配置 Jackson ObjectMapper

问题描述

我知道如何根据https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-theObjectMapper的应用程序属性在 Spring Boot中配置 Jackson -杰克逊对象映射器

我如何JsonFactory.Feature以这种方式控制杰克逊?

在 Spring 4.3.14 中,通过其默认构造函数进行Jackson2ObjectMapperBuilder实例化ObjectMapper,即不传递JsonFactory. 这种行为后来明显改变/修复:https ://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder .java#L621

标签: springspring-bootjackson

解决方案


@Bean
public ObjectMapper tolerantObjectMapper() {
    log.info("Creating Object mapper");
    final JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
    return objectMapper;
}

推荐阅读