Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of declaring ObjectMapper as a bean?

Let's say I just want a plain instance of ObjectMapper object. Is there any advantage to declare it as a bean?

@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper();
}

Why not just make a new ObjectMapper by new ObjectMapper() every time we need it?

Or declare it as a static object?

private static final ObjectMapper mapper = new ObjectMapper();
like image 795
wltheng Avatar asked May 16 '18 05:05

wltheng


People also ask

What is the purpose of ObjectMapper in Java?

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.

What is the use of ObjectMapper in spring boot?

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.

What does Jackson ObjectMapper do?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.

Is the ObjectMapper thread safe?

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls.


1 Answers

Here is the API note about ObjectMapper

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls. If configuration of a mapper is modified after first usage, changes may or may not take effect, and configuration calls themselves may fail.

And here is guideline to improve jackson performance:

Reuse heavy-weight objects: ObjectMapper (data-binding) and JsonFactory (streaming API) To a lesser degree, you may also want to reuse ObjectReader and ObjectWriter instances -- this is just some icing on the cake, but they are fully thread-safe and reusable

So to summarize:

  • ObjectMapper is thread-safe, as long as you did not change your configuration on the fly

  • ObjectMapper initialization is a heavy operation

Therefore, declare your ObjectMapper as @Bean will:

  • Improve parsing performance (as you do not need to re-init the instance when parsing)

  • Reduce memory usage (less objects created)

  • Your ObjectMapper returned from @Bean method is fully configured. It is thread-safe. (But do obviously, do not modify the @Autowired instance XD)

  • Give common configuration for your application (like timezone, null fail-over config...)

like image 126
Mạnh Quyết Nguyễn Avatar answered Sep 19 '22 16:09

Mạnh Quyết Nguyễn