From b06882a526c685c3326f404e29d5db33af7dc1a2 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 9 Oct 2023 11:08:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=85=AC=E5=85=B1JSON=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/platform/api/AbstractPlatform.java | 4 +-- .../plugin/platform/api/BaseClient.java | 6 ++--- .../util/{JSONUtils.java => PluginUtils.java} | 9 +++++-- .../java/io/metersphere/sdk/util/JSON.java | 8 +++++- .../io/metersphere/sdk/util/LogUtils.java | 2 +- .../io/metersphere/api/util/JSONUtils.java | 26 +++++++------------ 6 files changed, 29 insertions(+), 26 deletions(-) rename backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/{JSONUtils.java => PluginUtils.java} (92%) diff --git a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java index c6b5aeef7c..4e73844763 100644 --- a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java +++ b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java @@ -1,7 +1,7 @@ package io.metersphere.plugin.platform.api; import io.metersphere.plugin.platform.dto.PlatformRequest; -import io.metersphere.plugin.sdk.util.JSONUtils; +import io.metersphere.plugin.sdk.util.PluginUtils; import io.metersphere.plugin.sdk.util.MSPluginException; import org.apache.commons.lang3.StringUtils; @@ -16,7 +16,7 @@ public abstract class AbstractPlatform implements Platform { if (StringUtils.isBlank(integrationConfig)) { throw new MSPluginException("服务集成配置为空"); } - return JSONUtils.parseObject(integrationConfig, clazz); + return PluginUtils.parseObject(integrationConfig, clazz); } public String getPluginId() { diff --git a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java index 253220a21d..cafa1a9d50 100644 --- a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java +++ b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java @@ -2,7 +2,7 @@ package io.metersphere.plugin.platform.api; import io.metersphere.plugin.platform.utils.EnvProxySelector; import io.metersphere.plugin.platform.utils.PluginCodingUtils; -import io.metersphere.plugin.sdk.util.JSONUtils; +import io.metersphere.plugin.sdk.util.PluginUtils; import io.metersphere.plugin.sdk.util.MSPluginException; import io.metersphere.plugin.sdk.util.PluginLogUtils; import org.apache.commons.lang3.StringUtils; @@ -87,11 +87,11 @@ public abstract class BaseClient { } protected Object getResultForList(Class clazz, ResponseEntity response) { - return Arrays.asList(JSONUtils.parseArray(getResult(response), clazz).toArray()); + return Arrays.asList(PluginUtils.parseArray(getResult(response), clazz).toArray()); } protected Object getResultForObject(Class clazz, ResponseEntity response) { - return JSONUtils.parseObject(getResult(response), clazz); + return PluginUtils.parseObject(getResult(response), clazz); } public void validateProxyUrl(String url, String... path) { diff --git a/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSONUtils.java b/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/PluginUtils.java similarity index 92% rename from backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSONUtils.java rename to backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/PluginUtils.java index c42c8a2593..dae5fb1d28 100644 --- a/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSONUtils.java +++ b/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/PluginUtils.java @@ -4,11 +4,13 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.StreamReadConstraints; +import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; @@ -17,8 +19,11 @@ import java.io.InputStream; import java.util.List; import java.util.Map; -public class JSONUtils { - private static final ObjectMapper objectMapper = new ObjectMapper(); +public class PluginUtils { + private static final ObjectMapper objectMapper = JsonMapper.builder() + .enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS) + .build(); + private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0; diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java index f044e1cf60..e88df43079 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java @@ -4,11 +4,13 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.StreamReadConstraints; +import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -19,8 +21,11 @@ import java.util.List; import java.util.Map; public class JSON { - private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final ObjectMapper objectMapper = JsonMapper.builder() + .enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS) + .build(); private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); + public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0; static { @@ -35,6 +40,7 @@ public class JSON { .setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(DEFAULT_MAX_STRING_LEN).build()); // 处理时间格式 objectMapper.registerModule(new JavaTimeModule()); + } public static String toJSONString(Object value) { diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java index 87b0c45c8a..3f5498cb1f 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/LogUtils.java @@ -18,7 +18,7 @@ public class LogUtils { /** * 初始化日志 */ - public static Logger getLogger() { + private static Logger getLogger() { return LoggerFactory.getLogger(LogUtils.getLogClass()); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/util/JSONUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/util/JSONUtils.java index 63fa0c8590..4c60e62924 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/util/JSONUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/util/JSONUtils.java @@ -2,10 +2,11 @@ package io.metersphere.api.util; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.jsontype.NamedType; import com.fasterxml.jackson.databind.jsontype.impl.StdSubtypeResolver; import com.fasterxml.jackson.databind.type.CollectionType; @@ -18,8 +19,11 @@ import java.util.LinkedList; import java.util.List; public class JSONUtils { - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static StdSubtypeResolver resolver = new StdSubtypeResolver(); + private static final ObjectMapper objectMapper = JsonMapper.builder() + .enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS) + .build(); + + private static final StdSubtypeResolver resolver = new StdSubtypeResolver(); static { // 添加处理资源文件的类 @@ -33,7 +37,7 @@ public class JSONUtils { // 如果一个对象中没有任何的属性,那么在序列化的时候就会报错 objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); - namedTypes.forEach(namedType -> resolver.registerSubtypes(namedType)); + namedTypes.forEach(resolver::registerSubtypes); objectMapper.setSubtypeResolver(resolver); } @@ -65,18 +69,6 @@ public class JSONUtils { } } - public static T parseObject(String content, TypeReference valueType) { - try { - return objectMapper.readValue(content, valueType); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public static List parseArray(String content) { - return parseArray(content, Object.class); - } - public static List parseArray(String content, Class valueType) { CollectionType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class, valueType); try { @@ -92,7 +84,7 @@ public class JSONUtils { * @param namedTypes */ public static void setResolver(List namedTypes) { - namedTypes.forEach(namedType -> resolver.registerSubtypes(namedType)); + namedTypes.forEach(resolver::registerSubtypes); objectMapper.setSubtypeResolver(resolver); } }