项目里面一直在用 gson 来序列化对象,最近在接一家第三方 api 。
如果请求出错,对方响应类似:
{"code":-1021,"msg":"Timestamp for this request is outside of the recvWindow."}
请求正确,对方响应:
{
"feeTier": 0,
"updateTime": 0,
"assets": [{
"asset": "XXX",
"updateTime": 0
}],
"positions": [{
"symbol": "XXX",
"initialMargin": "0",
"maintMargin": "0"
}]
}
我自己构造了 2 个对象的 Pojo ,一个是 ResponseError ,一个是 Response
if( httpResponse.contains("code\":-1021")){
ResponseError resp = GSON.fromJson( httpResponse, ResponseError.class);
}else{
Response resp = GSON.fromJson( httpResponse, Response.class);
}
不知道有没有更好的写法,能够定义一个更好的: 1 ,让我能够不先用 string 来判断。。。 2 ,最好 2 个响应的序列化的对象可以怎样继承一下,统一的返回给上层。。。