Spring RestTemplate获取通用对象

时间:2016-07-20 06:52:36

标签: spring generics 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

当我从服务器上获得这个模型时,我认为它是

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吗?

一些事情,这个

ResponseMessage<LkAuth[]> model = restTemplate.getForObject(lkAuthListUrl, ResponseMessage<LkAuth[]>.class);

0 个答案:

没有答案