1
This commit is contained in:
@@ -263,12 +263,16 @@ public class OtherUtil {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
// 使用 TypeReference 保留泛型信息,避免类型擦除
|
||||
return objectMapper.readValue(json, new TypeReference<List<T>>() {});
|
||||
// 注意:new TypeReference<List<T>>() {} 会因泛型擦除退化为原始 List,
|
||||
// 元素被反序列化为 LinkedHashMap,后续强转目标类型会抛 ClassCastException。
|
||||
// 必须用 TypeFactory 动态构建 List<T> 的 JavaType 才能保留元素类型。
|
||||
JavaType listType = objectMapper.getTypeFactory()
|
||||
.constructCollectionType(List.class, targetClass);
|
||||
return objectMapper.readValue(json, listType);
|
||||
} catch (Exception e) {
|
||||
System.err.println("解析 JSON 数组失败,json 前缀: " + json);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
throw new DException("解析 JSON 数组失败!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,9 +290,9 @@ public class OtherUtil {
|
||||
try {
|
||||
return objectMapper.readValue(json, targetClass);
|
||||
} catch (Exception e) {
|
||||
System.err.println("解析 JSON 数组失败,json 前缀: " + json);
|
||||
System.err.println("解析 JSON 对象失败,json 前缀: " + json);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
throw new DException("解析 JSON 对象失败!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,7 +309,7 @@ public class OtherUtil {
|
||||
} catch (Exception e) {
|
||||
System.err.println("解析 JSON 数组失败: " + json);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
throw new DException("解析 JSON 数组失败!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,7 +324,7 @@ public class OtherUtil {
|
||||
} catch (Exception e) {
|
||||
System.err.println("解析 JSON 数组失败: " + json);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
throw new DException("解析 JSON 数组失败!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user