Merge pull request #511 from dmego/fix_json_parse_bug

fix generics type cast exception
pull/515/head
小马哥 2 years ago committed by GitHub
commit a72c253654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,6 +17,8 @@
package cn.hippo4j.common.api; package cn.hippo4j.common.api;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List; import java.util.List;
/** /**
@ -42,6 +44,16 @@ public interface JsonFacade {
*/ */
<T> T parseObject(String text, Class<T> clazz); <T> T parseObject(String text, Class<T> clazz);
/**
* Parse object.
*
* @param text
* @param valueTypeRef
* @param <T>
* @return
*/
<T> T parseObject(String text, TypeReference<T> valueTypeRef);
/** /**
* Parse array. * Parse array.
* *

@ -18,6 +18,7 @@
package cn.hippo4j.common.toolkit; package cn.hippo4j.common.toolkit;
import cn.hippo4j.common.api.JsonFacade; import cn.hippo4j.common.api.JsonFacade;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List; import java.util.List;
@ -44,6 +45,14 @@ public class JSONUtil {
return JSON_FACADE.parseObject(text, clazz); return JSON_FACADE.parseObject(text, clazz);
} }
public static <T> T parseObject(String text, TypeReference<T> valueTypeRef) {
if (StringUtil.isBlank(text)) {
return null;
}
return JSON_FACADE.parseObject(text, valueTypeRef);
}
public static <T> List<T> parseArray(String text, Class<T> clazz) { public static <T> List<T> parseArray(String text, Class<T> clazz) {
if (StringUtil.isBlank(text)) { if (StringUtil.isBlank(text)) {
return null; return null;

@ -20,6 +20,7 @@ package cn.hippo4j.common.toolkit;
import cn.hippo4j.common.api.JsonFacade; import cn.hippo4j.common.api.JsonFacade;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
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;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -61,6 +62,12 @@ public class JacksonHandler implements JsonFacade {
return MAPPER.readValue(text, javaType); return MAPPER.readValue(text, javaType);
} }
@Override
@SneakyThrows
public <T> T parseObject(String text, TypeReference<T> valueTypeRef) {
return MAPPER.readValue(text, valueTypeRef);
}
@Override @Override
@SneakyThrows @SneakyThrows
public <T> List<T> parseArray(String text, Class<T> clazz) { public <T> List<T> parseArray(String text, Class<T> clazz) {

@ -30,6 +30,7 @@ import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterReqDTO;
import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO; import cn.hippo4j.config.model.biz.adapter.ThreadPoolAdapterRespDTO;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import cn.hutool.http.HttpUtil; import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -105,7 +106,7 @@ public class ThreadPoolAdapterService {
try { try {
String resultStr = HttpUtil.get(urlString, param, HTTP_EXECUTE_TIMEOUT); String resultStr = HttpUtil.get(urlString, param, HTTP_EXECUTE_TIMEOUT);
if (StringUtil.isNotBlank(resultStr)) { if (StringUtil.isNotBlank(resultStr)) {
Result<ThreadPoolAdapterRespDTO> restResult = JSONUtil.parseObject(resultStr, Result.class); Result<ThreadPoolAdapterRespDTO> restResult = JSONUtil.parseObject(resultStr, new TypeReference<Result<ThreadPoolAdapterRespDTO>>(){});
result.add(restResult.getData()); result.add(restResult.getData());
} }
} catch (Throwable ex) { } catch (Throwable ex) {

Loading…
Cancel
Save