java - Should I declare Jackson's ObjectMapper as a static field? - Stack Overflow
Yes, that is safe and recommended.
The only caveat from the page you referred is that you can't be modifying configuration of the mapper once it is shared; but you are not changing configuration so that is fine. If you did need to change configuration, you would do that from the static block and it would be fine as well.
EDIT: (2013/10)
With 2.0 and above, above can be augmented by noting that there is an even better way: use ObjectWriter
and ObjectReader
objects, which can be constructed by ObjectMapper
. They are fully immutable, thread-safe, meaning that it is not even theoretically possible to cause thread-safety issues (which can occur with ObjectMapper
iff code tries to re-configure instance).
Read full article from java - Should I declare Jackson's ObjectMapper as a static field? - Stack Overflow
No comments:
Post a Comment