fix generics type cast exception

pull/511/head
Dmego 2 years ago
parent ae66252709
commit c3ab689abc

@ -17,6 +17,8 @@
package cn.hippo4j.common.api;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List;
/**
@ -42,6 +44,16 @@ public interface JsonFacade {
*/
<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.
*

@ -18,6 +18,7 @@
package cn.hippo4j.common.toolkit;
import cn.hippo4j.common.api.JsonFacade;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List;
@ -44,6 +45,14 @@ public class JSONUtil {
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) {
if (StringUtil.isBlank(text)) {
return null;

@ -20,6 +20,7 @@ package cn.hippo4j.common.toolkit;
import cn.hippo4j.common.api.JsonFacade;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
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;
@ -61,6 +62,12 @@ public class JacksonHandler implements JsonFacade {
return MAPPER.readValue(text, javaType);
}
@Override
@SneakyThrows
public <T> T parseObject(String text, TypeReference<T> valueTypeRef) {
return MAPPER.readValue(text, valueTypeRef);
}
@Override
@SneakyThrows
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.hutool.core.text.StrBuilder;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
@ -105,7 +106,7 @@ public class ThreadPoolAdapterService {
try {
String resultStr = HttpUtil.get(urlString, param, HTTP_EXECUTE_TIMEOUT);
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());
}
} catch (Throwable ex) {

Loading…
Cancel
Save