diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/JacksonImpl.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/JacksonImpl.java index 449d0c0d839..d58a6ef64c2 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/JacksonImpl.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/impl/JacksonImpl.java @@ -19,6 +19,7 @@ import org.apache.dubbo.common.extension.Activate; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonInclude.Include; @@ -26,6 +27,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.json.JsonMapper.Builder; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -34,6 +36,7 @@ public class JacksonImpl extends AbstractJsonUtilImpl { private volatile JsonMapper mapper; + private final List customModules = new ArrayList<>(); @Override public String getName() { @@ -113,10 +116,24 @@ protected JsonMapper getMapper() { } protected Builder createBuilder() { - return JsonMapper.builder() + Builder builder = JsonMapper.builder() .configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .serializationInclusion(Include.NON_NULL) .addModule(new JavaTimeModule()); + + for (Module module : customModules) { + builder.addModule(module); + } + + return builder; + } + + public void addModule(Module module) { + synchronized (this) { + customModules.add(module); + // Invalidate the mapper to rebuild it + this.mapper = null; + } } }