spring - 使用 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
解决方案
@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;
}
推荐阅读
- c - 带链表 C 的冒泡排序
- android - 不同会话中 PWA 的可信 Web 活动
- docker - 可以将文件夹挂载到docker容器的根目录中吗
- python - 如何检查 Python/HTML 表单中的重复用户输入?
- ios - Fastlane iOS 自动通知截图
- java - 拆分器中的 action.accept(val) 做什么
- python - 使用 ODEINT 或其他方法求解许多耦合微分方程组
- javascript - 简单的函数在使用 onclick 时触发,在 JS 中使用 addEventListener 时不会触发
- nginx - 每次添加第二个虚拟主机后,NGINX 重启都会失败
- node.js - Mongo Aggregate - 获取查询结果中出现多个值的次数