實際工作中,我們經(jīng)常對接口調(diào)用進行封裝桦他,利用泛型返回結(jié)果值搔预。
1. 封裝函數(shù)霹期,統(tǒng)一的返回結(jié)果值
所有的調(diào)用接口封裝成一個公共函數(shù),函數(shù)返回一個泛型類拯田。如:
public class ListResultDto<T> extends Dto
{
private static final long serialVersionUID = -7187882293150693618L;
private static final long serialVersionUID = -557141271464869806L;
//返回狀態(tài)標識
private int rtnCode;
//返回話術(shù)
private String rtnMsg = RespConstants.SUCCESS_MSG;
//數(shù)據(jù)結(jié)果
private List<T> result;
}
//提供泛型方法
/**
* 將字符串轉(zhuǎn)化成bean對象
*/
protected <T> T changeJSON2Bean(Class className, String jsonString)
{
if (StringUtil.isNotNullEmpty(jsonString))
{
return (T) JSON.parseObject(jsonString, className);
}
return null;
}
//調(diào)用封裝函數(shù)賦值
public ListResultDto<GlobalNewGoods> getGlobalNewGoodsRecommend(ModuleRequest req)
{
String apiUrl = RestConstants.App.Controller + RestConstants.App.GlobalNewGoodsRecommend;
//封裝函數(shù),返回泛型
return requestApi(req, apiUrl, ListResultDto.class);
}
2. 容易出錯的地方
泛型取值的過程中历造,比較容易犯錯的的地方是json字符串轉(zhuǎn)換對象,一定要按類型轉(zhuǎn)換船庇。使用fastJson的時候?qū)τ诜盒偷姆葱蛄谢芏鄨鼍跋露紩褂玫絋ypeReference
錯誤的做法:
public static ResultDto<JbOrderStatus> getJbOrderStatus(GlobalOrder tbOrder)
{
JSONObject json = new JSONObject();
String url = "getOrderStatus";
ResultDto<JbOrderStatus> dto = requestThird(JSON.toJSONString(tbOrder), url, ResultDto.class);
return dto;
}
正確的做法:
//返回json字符串
String result =requestThird(jsonObject.toJSONString(), url);
//ResultDto泛型類吭产,指定按類型轉(zhuǎn)換
ResultDto<JbOrderStatus> dto = JSONObject.parseObject(result, new TypeReference<ResultDto<JbOrderStatus>>(){});
//ResultDto泛型類,指定按類型轉(zhuǎn)換
ListResultDto<TbGoodsSku> dto = JSONObject.parseObject(result, new TypeReference<ListResultDto<TbGoodsSku>>(){});
類似map鸭轮,List等泛型臣淤,也是如此:
public static void main(String[] args) {
Map<String, Person> map = new HashMap<>(16);
map.put("one", new Person("zhangsan"));
map.put("two", new Person("lisi"));
String jsonStr = JSON.toJSONString(map);
byte[] bytes = jsonStr.getBytes();
String json = new String(bytes);
Map<String, Person> res = JSON.parseObject(json, Map.class);
System.out.println(res.get("one"));
System.out.println(res.get("one").getName());
}
執(zhí)行時異常:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:67)
Caused by: java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to subtitle.io.Person
反序列化時候,雖然添加Map.class窃爷,但是沒有辦法指定Person類型邑蒋,導(dǎo)致反序列化后的對象為Map<String, Map<String, String>>,而不是Map<String, Person>,所以針對泛型的反序列化按厘,需要使用TypeReference寺董。
Map<String, Person> res = JSON.parseObject(json, new TypeReference<Map<String, Person>>(){});
System.out.println(res.get("one"));
System.out.println(res.get("one").getName());