refactor: 重复工具类处理

This commit is contained in:
fit2-zhao 2023-08-31 09:49:31 +08:00 committed by fit2-zhao
parent 912f8b24a0
commit 19ba5fc714
5 changed files with 21 additions and 7 deletions

View File

@ -1,7 +1,7 @@
package io.metersphere.plugin.platform.api; package io.metersphere.plugin.platform.api;
import io.metersphere.plugin.platform.dto.PlatformRequest; import io.metersphere.plugin.platform.dto.PlatformRequest;
import io.metersphere.plugin.sdk.util.JSON; import io.metersphere.plugin.sdk.util.JSONUtils;
import io.metersphere.plugin.sdk.util.MSPluginException; import io.metersphere.plugin.sdk.util.MSPluginException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -16,7 +16,7 @@ public abstract class AbstractPlatform implements Platform {
if (StringUtils.isBlank(integrationConfig)) { if (StringUtils.isBlank(integrationConfig)) {
throw new MSPluginException("服务集成配置为空"); throw new MSPluginException("服务集成配置为空");
} }
return JSON.parseObject(integrationConfig, clazz); return JSONUtils.parseObject(integrationConfig, clazz);
} }
public String getPluginId() { public String getPluginId() {

View File

@ -2,7 +2,7 @@ package io.metersphere.plugin.platform.api;
import io.metersphere.plugin.platform.utils.EnvProxySelector; import io.metersphere.plugin.platform.utils.EnvProxySelector;
import io.metersphere.plugin.platform.utils.PluginCodingUtils; import io.metersphere.plugin.platform.utils.PluginCodingUtils;
import io.metersphere.plugin.sdk.util.JSON; import io.metersphere.plugin.sdk.util.JSONUtils;
import io.metersphere.plugin.sdk.util.MSPluginException; import io.metersphere.plugin.sdk.util.MSPluginException;
import io.metersphere.plugin.sdk.util.PluginLogUtils; import io.metersphere.plugin.sdk.util.PluginLogUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -87,11 +87,11 @@ public abstract class BaseClient {
} }
protected Object getResultForList(Class clazz, ResponseEntity<String> response) { protected Object getResultForList(Class clazz, ResponseEntity<String> response) {
return Arrays.asList(JSON.parseArray(getResult(response), clazz).toArray()); return Arrays.asList(JSONUtils.parseArray(getResult(response), clazz).toArray());
} }
protected Object getResultForObject(Class clazz, ResponseEntity<String> response) { protected Object getResultForObject(Class clazz, ResponseEntity<String> response) {
return JSON.parseObject(getResult(response), clazz); return JSONUtils.parseObject(getResult(response), clazz);
} }
public void validateProxyUrl(String url, String... path) { public void validateProxyUrl(String url, String... path) {

View File

@ -3,6 +3,7 @@ package io.metersphere.plugin.sdk.util;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
@ -16,9 +17,10 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class JSON { public class JSONUtils {
private static final ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectMapper objectMapper = new ObjectMapper();
private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); private static final TypeFactory typeFactory = objectMapper.getTypeFactory();
public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0;
static { static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@ -27,6 +29,9 @@ public class JSON {
// 如果一个对象中没有任何的属性那么在序列化的时候就会报错 // 如果一个对象中没有任何的属性那么在序列化的时候就会报错
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// 设置JSON处理字符长度限制
objectMapper.getFactory()
.setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(DEFAULT_MAX_STRING_LEN).build());
} }
public static String toJSONString(Object value) { public static String toJSONString(Object value) {

View File

@ -3,6 +3,7 @@ package io.metersphere.sdk.util;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
@ -10,6 +11,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -19,6 +21,7 @@ import java.util.Map;
public class JSON { public class JSON {
private static final ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectMapper objectMapper = new ObjectMapper();
private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); private static final TypeFactory typeFactory = objectMapper.getTypeFactory();
public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0;
static { static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@ -27,6 +30,12 @@ public class JSON {
// 如果一个对象中没有任何的属性那么在序列化的时候就会报错 // 如果一个对象中没有任何的属性那么在序列化的时候就会报错
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// 设置JSON处理字符长度限制
objectMapper.getFactory()
.setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(DEFAULT_MAX_STRING_LEN).build());
// 处理时间格式
objectMapper.registerModule(new JavaTimeModule());
} }
public static String toJSONString(Object value) { public static String toJSONString(Object value) {

View File

@ -1,11 +1,11 @@
package io.metersphere.system.controller; package io.metersphere.system.controller;
import io.metersphere.plugin.platform.api.AbstractPlatformPlugin; import io.metersphere.plugin.platform.api.AbstractPlatformPlugin;
import io.metersphere.plugin.sdk.util.JSON;
import io.metersphere.sdk.base.BaseTest; import io.metersphere.sdk.base.BaseTest;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.service.PluginLoadService; import io.metersphere.sdk.service.PluginLoadService;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.controller.param.ServiceIntegrationUpdateRequestDefinition; import io.metersphere.system.controller.param.ServiceIntegrationUpdateRequestDefinition;
import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Organization;
import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.Plugin;