public xxxcountOfType(Map map){
System.out.println("前端穿的ownerId是:" + map.get("ownerId") );
System.out.println("前端穿的数组是" + map.get("list"));
Object ownerId = map.get("ownerId");
ArrayList ids = (ArrayList)map.get("list");
......
}
代码运行到ArrayList
io.openflat.core.ApiInvoker.caugthException(ApiInvoker.java:185) com.alibaba.fastjson.JSONArray cannot be cast to java.util.ArrayList
method: 'post',
name: 'xxxx.countOfType',
async: false,
data:{'list':me.ids,'ownerId':data[0].ownerId},
将ArrayList
改为List
将ArrayList
改为
ArrayList ids = JSONObject.parseObject(JSONObject.toJSONString(map.get("list")),ArrayList.class);
将ArrayList ids = (ArrayList)map.get("list");
改为
ArrayList ids = JSONObject.parseObject(map.get("list").toString(),new TypeReference>(){});
原文链接:https://blog.csdn.net/zhuzj12345/article/details/102914545
在使用fastJson时,对于泛型的反序列化很多场景下都会使用到TypeReference,例如:
void testTypeReference() {
List list = new ArrayList<>();
list.add(1);
list.add(9);
list.add(4);
list.add(8);
JSONObject jsonObj = new JSONObject();
jsonObj.put("a", list);
System.out.println(jsonObj);
List list2 = jsonObj.getObject("a", new TypeReference>(){});
System.out.println(list2);
}
输出
1{"a":[1,9,4,8]}
2[1, 9, 4, 8]
使用TypeReference可以明确的指定反序列化的类型,具体实现逻辑参考TypeReference的构造函数
protected TypeReference(){
Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
Type cachedType = classTypeCache.get(type);
if (cachedType == null) {
classTypeCache.putIfAbsent(type, type);
cachedType = classTypeCache.get(type);
}
this.type = cachedType;
}
其中核心的方法是:getActualTypeArguments,它可以得到父类的反省类型
ParameterizedType是一个记录类型泛型的接口, 继承自Type,一共三方法:
例如 Map
对应的ParameterizedType三个方法分别取值如下:
package JsonLearn;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
public class TypeReferencBaseLearn {
public static class IntMap extends HashMap {}
void test1() {
IntMap intMap = new IntMap();
System.out.println("getSuperclass:" + intMap.getClass().getSuperclass());
System.out.println("getGenericSuperclass:" + intMap.getClass().getGenericSuperclass());
Type type = intMap.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType)type;
for (Type t : p.getActualTypeArguments()) {
System.out.println(t);
}
}
}
void test2() {
Map intMap = new HashMap<>();
System.out.println("\ngetSuperclass:" + intMap.getClass().getSuperclass());
System.out.println("getGenericSuperclass:" + intMap.getClass().getGenericSuperclass());
Type type = intMap.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType)type;
for (Type t : p.getActualTypeArguments()) {
System.out.println(t);
}
}
}
void test3() {
Map intMap = new HashMap(){};
System.out.println("\ngetSuperclass:" + intMap.getClass().getSuperclass());
System.out.println("getGenericSuperclass:" + intMap.getClass().getGenericSuperclass());
Type type = intMap.getClass().getGenericSuperclass();
if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType)type;
for (Type t : p.getActualTypeArguments()) {
System.out.println(t);
}
}
}
public static void main(String[] args) {
TypeReferencBaseLearn obj = new TypeReferencBaseLearn();
obj.test1();
obj.test2();
obj.test3();
}
}
输出
getSuperclass:class java.util.HashMap
getGenericSuperclass:java.util.HashMap
class java.lang.String
class java.lang.Integer
getSuperclass:class java.util.AbstractMap
getGenericSuperclass:java.util.AbstractMap
K
V
getSuperclass:class java.util.HashMap
getGenericSuperclass:java.util.HashMap
class java.lang.String
class java.lang.Integer
FastJson的相关应用可参考:https://blog.csdn.net/beidaol/article/details/104491950