Languages

Java - Is it thread-safe to declare Jackson's ObjectMapper as a static field?

8 points
Asked by:
Tomeriko
413

I am confused about using Jackson's com.fasterxml.jackson.databind.ObjectMapper class instance as static field in the class because of possible thread-safety issues.

I have found in information where thread-safety is confirmed (Using Jackson Object Mapper).

Is it true?

1 answer
7 points
Answered by:
Root-ssh
178180

Yes, it is true.

Even more, it is recommended to use com.fasterxml.jackson.databind.ObjectMapper class instance as static field during entire lifetime of application when it is possible. The reasone why the mapper class is not static is giving user option to use different configurations per mapper instance.

From documentation:

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

Sources:

  1. https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/ObjectMapper.html
  2. https://access.redhat.com/webassets/avalon/d/red-hat-jboss-enterprise-application-platform/7.1.beta/javadocs/com/fasterxml/jackson/databind/ObjectMapper.html
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join