refactor: 公共JSON处理优化
This commit is contained in:
parent
d715d1f569
commit
b06882a526
|
@ -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() {
|
||||
|
|
|
@ -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<String> 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<String> response) {
|
||||
return JSONUtils.parseObject(getResult(response), clazz);
|
||||
return PluginUtils.parseObject(getResult(response), clazz);
|
||||
}
|
||||
|
||||
public void validateProxyUrl(String url, String... path) {
|
||||
|
|
|
@ -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;
|
||||
|
|
@ -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) {
|
||||
|
|
|
@ -18,7 +18,7 @@ public class LogUtils {
|
|||
/**
|
||||
* 初始化日志
|
||||
*/
|
||||
public static Logger getLogger() {
|
||||
private static Logger getLogger() {
|
||||
return LoggerFactory.getLogger(LogUtils.getLogClass());
|
||||
}
|
||||
|
||||
|
|
|
@ -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> T parseObject(String content, TypeReference<T> 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 <T> List<T> parseArray(String content, Class<T> valueType) {
|
||||
CollectionType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class, valueType);
|
||||
try {
|
||||
|
@ -92,7 +84,7 @@ public class JSONUtils {
|
|||
* @param namedTypes
|
||||
*/
|
||||
public static void setResolver(List<NamedType> namedTypes) {
|
||||
namedTypes.forEach(namedType -> resolver.registerSubtypes(namedType));
|
||||
namedTypes.forEach(resolver::registerSubtypes);
|
||||
objectMapper.setSubtypeResolver(resolver);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue