RestClient.java 3.8 KB
package com.cjs.cms.util.net;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.cjs.cms.util.lang.JsonUtil;

/**
 * Rest接口客户端工具类
 * 
 * @author tongyufu
 *
 */
@Component
public class RestClient extends RestTemplate implements RestOperation {

    public RestClient() {
        //连接超时
        ((SimpleClientHttpRequestFactory) this.getRequestFactory()).setConnectTimeout(20000);
        //读取超时
        ((SimpleClientHttpRequestFactory) this.getRequestFactory()).setReadTimeout(20000);
        ClientHttpRequestInterceptor interceptor = new RestClientHttpRequestInterceptor();
        this.setInterceptors(Collections.singletonList(interceptor));
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getForList(String url, Class<T> elementType, Object... urlVariables) {
        String response = super.getForObject(url, String.class, urlVariables);
        return JsonUtil.fromJson(response, List.class, elementType);
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getForList(String url, Class<T> elementType, Map<String, ?> urlVariables) {
        String response = super.getForObject(url, String.class, urlVariables);
        return JsonUtil.fromJson(response, List.class, elementType);
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> postForList(String url, Object request, Class<T> elementType,
                                   Object... urlVariables) {
        String response = super.postForObject(url, request, String.class, urlVariables);
        return JsonUtil.fromJson(response, List.class, elementType);
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> postForList(String url, Object request, Class<T> elementType,
                                   Map<String, ?> urlVariables) {
        String response = super.postForObject(url, request, String.class, urlVariables);
        return JsonUtil.fromJson(response, List.class, elementType);
    }

    /**
     * 使用POST请求发送JSON字符串
     * @param url 请求路径
     * @param request 请求体
     * @param responseType 返回值类型
     * @return
     */
    public <T> T postForJson(String url, String request, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", HttpUtil.CHARSET));
        HttpEntity<String> entity = new HttpEntity<String>(request, headers);
        String response = super.exchange(url, HttpMethod.POST, entity, String.class).getBody();
        return JsonUtil.fromJson(response, responseType);
    }

    /**
     * 使用POST请求发送JSON字符串
     * @param url 请求路径
     * @param request 请求体
     * @param responseType 返回值类型
     * @return
     */
    public <L extends Collection<E>, E> L postForJson(String url, String request,
                                                      Class<L> collectionClass,
                                                      Class<E> elementClass) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", HttpUtil.CHARSET));
        HttpEntity<String> entity = new HttpEntity<String>(request, headers);
        String response = super.exchange(url, HttpMethod.POST, entity, String.class).getBody();
        return JsonUtil.fromJson(response, collectionClass, elementClass);
    }

}