如何获取 Spring 4.1 使用的 Jackson ObjectMapper? [英] How do I obtain the Jackson ObjectMapper in use by Spring 4.1?
问题描述
Spring 4.1 实例化一个 Jackson ObjectMapper
实例.我有理由想要将该实例 @Autowire
放入我的一个控制器中:控制器使用 Jackson 对它自己进行了一些次要的 JSON 解析,但它使用的 ObjectMapper
应该是Spring 本身正在使用的同一个实例.我该怎么做?
Spring 4.1 instantiates a Jackson ObjectMapper
instance. I have reason to want to @Autowire
that instance into one of my controllers: The controller does some minor JSON parsing of its own using Jackson, but the ObjectMapper
it uses should be the one and same instance that Spring itself is using. How do I go about accomplishing that?
请注意,我不是在问如何自定义配置 Spring 使用的 ObjectMapper
;我对默认设置很满意.我只是想把 Spring 使用的实例捞出来,这样我就可以在自己的代码中重新使用现有的实例.
Note that I'm not asking how to custom configure the ObjectMapper
in use by Spring; I'm happy with the defaults. I just want to fish the instance used by Spring out so that I can re-use the existing instance in my own code.
推荐答案
如果您在类路径上使用 Spring Boot 和 Jackson,并且在 REST 控制器中使用默认的 JSON 解析实现,那么这应该有效:
If you're using Spring Boot with Jackson on your classpath and default implementation for JSON parsing in your REST controller, then this should work:
@Autowired
private ObjectMapper jacksonObjectMapper;
这篇关于如何获取 Spring 4.1 使用的 Jackson ObjectMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!