2
我給這個模型春RestTemplate獲得通用對象
public class ResponseMessage<T> {
private T result;
private ResultCode resultCode;
private String details;
public ResponseMessage(T result, ResultCode resultCode, String details) {
this.result = result;
this.resultCode = resultCode;
this.details = details;
}
//getters and setters
,當我從服務器獲取這個模型我conwert它
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(lkAuthListUrl, String.class);
Type listType = new TypeToken<ResponseMessage<LkAuth[]>>() {
}.getType();
ResponseMessage<LkAuth[]> model = Converter.me().getModel(result, listType);
,並在轉換器我做
public <T> T getModel(String jsonStr, Type type) {
T t = null;
try {
t = gson.fromJson(jsonStr, type);
} catch (Exception e) {
System.out.print(e);
}
return t;
}
我可以在我的方法中避免轉換並將此職責轉移到RestTemplate
?
東西LILE此
ResponseMessage<LkAuth[]> model = restTemplate.getForObject(lkAuthListUrl, ResponseMessage<LkAuth[]>.class);