Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I obtain the Jackson ObjectMapper in use by Spring 4.1?

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?

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.

like image 725
Adam Maass Avatar asked May 05 '15 17:05

Adam Maass


People also ask

Does spring boot have Jackson ObjectMapper?

Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.

Is Jackson Databind included in spring boot?

If you use Spring Boot, the jackson-databind dependency comes in the spring-boot-starter-json module (which also is included in other spring boot started moduled, like spring-boot-starter-web ).

What is ObjectMapper class in Jackson?

ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.

Can ObjectMapper be reused?

Note that copy() operation is as expensive as constructing a new ObjectMapper instance: if possible, you should still pool and reuse mappers if you intend to use them for multiple operations.


1 Answers

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; 
like image 79
Tuno Avatar answered Sep 23 '22 06:09

Tuno