RestClientUtils

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * REST接口客户端工具类
 * 
 * @author yy
 * @date 2018年2月13日
 */
@Component
public class RestClientUtils {

    @Autowired
    private RestTemplate restTemplate;
    private static RestClientUtils restClientUtils;

    @PostConstruct
    public void init() {
        restClientUtils = this;
        restClientUtils.restTemplate = this.restTemplate;
    }
    
    /**
     * get方式提交请求
     * @param url
     *         请求URL
     * @param clazz
     *         请求响应的类型
     * @param urlVariables
     *         URL变量列表
     * @return
     */
    public static <T> T getForObject(String url, Class<T> clazz, Map<String, Object> urlVariables) {
        return restClientUtils.restTemplate.getForObject(url, clazz, urlVariables);
    }

    /**
     * post方式提交请求并且携带header信息
     * @param url
     *         请求URL
     * @param headsMap
     *         携带的HTTP Header参数列表
     * @param body
     *         消息体
     * @return
     */
    public static String postForObject(String url, Map<String, String> headsMap, String body) {
        HttpHeaders headers = new HttpHeaders();
        for (String key : headsMap.keySet()) {
            headers.add(key, headsMap.get(key));
        }
        HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
        String result = restClientUtils.restTemplate.postForObject(url, httpEntity, String.class);
        return result;
    }
    
    /**
     * post方式提交请求并且携带header信息
     * @param url
     *         请求URL
     * @param headsMap
     *         携带的HTTP Header参数列表
     * @param body
     *         消息体
     * @return
     */
    public static String getForObject(String url, Map<String, String> headsMap, String body) {
        HttpHeaders headers = new HttpHeaders();
        for (String key : headsMap.keySet()) {
            headers.add(key, headsMap.get(key));
        }
        HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
        String result = restClientUtils.restTemplate.getForObject(url, String.class);
        return result;
    }
    
    /**
     * post方式提交请求
     * @param url
     *         请求URL
     * @param body
     *         消息体
     * @return
     */
    public static String postForString(String url, String body) {
        HttpHeaders headers = new HttpHeaders();
        //setAccessToken(headers,request);
        MediaType type = MediaType
                .parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("accessType","1");
        
        HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
        String result = restClientUtils.restTemplate.postForObject(url, formEntity,
                String.class);
        if (result == null)
            return null;
        return result;
    }

}
原文地址:https://www.cnblogs.com/wangquanyi/p/11640824.html