1

From ObjectMapper javadoc, it mentions ObjectMapper could be referred as static singleton or inject once and reuse the same instance.

However if I reuse static final ObjectMapper instance, will it hit with long lock due to internal synchronization? For example:-

public class SomeUtil {

    private static final ObjectMapper mapper;

    static{
        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static <T> T toObject(String json, Class<T> objectClass){

        try {
            return mapper.readValue(json, objectClass);
        } catch (IOException e) {
            log.error("Exception", e);
            return null;
        }
    }

    //surpress default ctor
    private SomeUtils(){}
}

Example code above is called by many services, adapters and etc concurrently. Most articles I read emphasise on thread-safety however I could not find satisfactory explanation on long synchronization lock issue. I appreciate for any insights in advance.

Awan Biru
  • 373
  • 2
  • 10

0 Answers0