问题描述
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?
请注意,我不是在问如何自定义配置 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 with 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!