Commit 9018c33d authored by liaozan's avatar liaozan 🏀

Fix possible concurrent issues

parent 67b39843
package com.schbrain.common.util; package com.schbrain.common.util;
import cn.hutool.core.lang.Singleton;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.*;
...@@ -27,17 +28,19 @@ public class JacksonUtils { ...@@ -27,17 +28,19 @@ public class JacksonUtils {
private static ObjectMapper PRETTY_OBJECT_MAPPER; private static ObjectMapper PRETTY_OBJECT_MAPPER;
public static ObjectMapper getObjectMapper() { public static ObjectMapper getObjectMapper() {
if (OBJECT_MAPPER == null) { return Singleton.get(JacksonUtils.class.getName(), () -> {
// Delay to get ObjectMapper from spring container to keep the same behavior with application if (OBJECT_MAPPER == null) {
try { // Delay to get ObjectMapper from spring container to keep the same behavior with application
OBJECT_MAPPER = SpringUtil.getBean(ObjectMapper.class).copy(); try {
} catch (Exception e) { OBJECT_MAPPER = SpringUtil.getBean(ObjectMapper.class).copy();
log.warn("Could not get ObjectMapper from Spring Container, return new instance for use"); } catch (Exception e) {
OBJECT_MAPPER = new ObjectMapper(); log.warn("Could not get ObjectMapper from Spring Container, return new instance for use");
OBJECT_MAPPER = new ObjectMapper();
}
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
} }
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); return OBJECT_MAPPER;
} });
return OBJECT_MAPPER;
} }
public static ObjectMapper getPrettyObjectMapper() { public static ObjectMapper getPrettyObjectMapper() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment