1. 程式人生 > >RestTemplate中使用ParameterizedTypeReference引數化型別支援泛型,主要是List

RestTemplate中使用ParameterizedTypeReference引數化型別支援泛型,主要是List

在使用http請求時,往往會封裝一個統一的結果類

 package com.plateno.booking.sync.ebk.process.http.response;

    public class InternalDataServiceResult<T> {
        private String resultCode;
        private String resultMsg;
        private T data;

        public String getResultCode() {
            return resultCode;
        }

        public void setResultCode(String resultCode) {
            this.resultCode = resultCode;
        }

        public String getResultMsg() {
            return resultMsg;
        }

        public void setResultMsg(String resultMsg) {
            this.resultMsg = resultMsg;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return "InternalDataServiceResult [resultCode=" + resultCode + ", resultMsg=" + resultMsg + ", data=" + data + "]";
        }

        /**
         * 是否操作成功 * @return
         */
        public boolean isSuccess() {
            return "0".equals(this.resultCode);
        }
    }

data是實際返回的資料(程式需要的),這裡是個T泛型,當T是類似List時,使用普通的RestTemplate.postForObject是無法知道OrderInnDay的型別的,最終T會是List,這不是我們想要的結果,恰好RestTemplate有其它的方法可以代替,比較麻煩(怪spring沒有很好的封裝上層方法)

 package com.plateno.booking.util; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import com.plateno.booking.bean.InternalDataServiceResult;

    @Component
    @Lazy(false)
    public class RestTemplateUtil {
        @Autowired
        private RestTemplate xRestTemplate;
        private static RestTemplate restTemplate;

        public static <T> InternalDataServiceResult<List<T>> exchangeAsList(HttpMethod method, String url, Map<String, Object> params, ParameterizedTypeReference<InternalDataServiceResult<List<T>>> responseType) {
            HttpEntity requestEntity = new HttpEntity(params);
            return restTemplate.exchange(url, method, requestEntity, responseType).getBody();
        }

        public static <T> InternalDataServiceResult<List<T>> postForList(String url, Map<String, Object> params, ParameterizedTypeReference<InternalDataServiceResult<List<T>>> responseType) {
            return exchangeAsList(HttpMethod.POST, url, params, responseType);
        }

        @PostConstruct
        public void init() {
            restTemplate = this.xRestTemplate;
        }

        public static RestTemplate getRestTemplate() {
            return restTemplate;
        }

        public static void setRestTemplate(RestTemplate restTemplate) {
            RestTemplateUtil.restTemplate = restTemplate;
        }

        public RestTemplate getxRestTemplate() {
            return xRestTemplate;
        }

        public void setxRestTemplate(RestTemplate xRestTemplate) {
            this.xRestTemplate = xRestTemplate;
        }
    }

當需要使用post請求,返回List時,即可這樣使用:

   public List<OrderInnDay> getOrderInnDayList(String orderCode, Integer memberId) {
        Map<String, Object> params = new HashMap<>();
        params.put("orderCode", orderCode);
        params.put("memberId", memberId);
        String url = apiProperties.getInternalDataService().getGetOrderInnDayList();
        log.info("query orderInnDayList,url:{},params:{}", url, JsonUtil.toJson(params)); //use postForList 
        nternalDataServiceResult<List<OrderInnDay>> result = RestTemplateUtil.postForList(url, params, new ParameterizedTypeReference<InternalDataServiceResult<List<OrderInnDay>>>() {
        });
        if (!result.isSuccess()) {
            log.warn("query orderInnDayList failed:url:{},params:{},response:{}", url, JsonUtil.toJson(params), JsonUtil.toJson(result));
        }
        return result.getData();
    }