javajackson

Should I declare Jackson's ObjectMapper every time I need it?


I have a multithread Java application, where I use the Jackson library. Everytime I need the ObjectMapper I declare it in the classes where I need to use it. Sometimes I customize this ObjectMapper with objectMapper.configure(), but not always. Is this a bad practice? Is there any way to declare it only one or two times?


Solution

  • I found that the instantiation new ObjectMapper() takes quite long, so you should definitely reuse the object rather than creating it every time before repeated use.

    I usually declare it like a logger in the class that'S using it (but of course it depends on your specific requirements whether this makes sense for you):

      private static final ObjectMapper objectMapper = new ObjectMapper();