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;
Answer from Tuno on Stack Overflowjava - How do I obtain the Jackson ObjectMapper in use by Spring 4.1? - Stack Overflow
Migration to Spring Boot 4.x: What are the hidden pitfalls you've encountered?
Performance benchmark of JSON parsers: Jackson vs Gson vs Boon
Jackson, moshi or kotlinx.serialization?
Kotlinx serialization is so elegant and nice, I really love it. I love it so much that I spent some time on making toml serialization library based on it: https://github.com/akuleshov7/ktoml
More on reddit.comIf 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;
As was said by others, you can't @Autowired it in directly into your controller.
@Emerson Farrugia's suggestion to create a new instance using
Jackson2ObjectMapperBuilder.json().build()
also didn't work for me because the obtained instance was not following the spring.jackson.* configuration properties, which I needed it to.
The solution I found was to obtain the ObjectMapper from Spring's MappingJackson2HttpMessageConverter which is injectable.
So I autowired it:
@Autowired
private MappingJackson2HttpMessageConverter springMvcJacksonConverter;
and then get the ObjectMapper from it like this:
ObjectMapper objectMapper = springMvcJacksonConverter.getObjectMapper();
This instance behaves exactly as Spring MVC's own message conversion - it probably is the same instance anyway.