All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.doudou.common.tool.utils.RestTemplateUtils Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.tool.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import top.doudou.common.tool.exception.ConnectionException;

import java.util.Map;

/**
 * @author  傻男人<[email protected]>
 * @Date: 2020/4/20
 * @Description:
 */
@Configuration
public class RestTemplateUtils {

    private static RestTemplate restTemplate = getInstance();

    public static RestTemplate getInstance() {
        if (restTemplate == null) {
            synchronized (RestTemplateUtils.class) {
                if (restTemplate == null) {
                    restTemplate = new RestTemplate();
                    restTemplate.setRequestFactory(crateClientHttpRequestFactory());
                }
            }
        }
        return restTemplate;
    }

    private static ClientHttpRequestFactory crateClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(10000);
        factory.setConnectTimeout(10000);
        return factory;
    }

    // ---------------------------------------------------------------------------------------------

    public T fromPost(String url, Map parameters,Class responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        return fromPost(url, parameters, headers,responseType);
    }

    public T fromPost(String url, Map parameters, HttpHeaders headers,Class responseType) {
        MultiValueMap map = new LinkedMultiValueMap<>();
        for (Map.Entry map1 : parameters.entrySet()) {
            if (null == map1.getValue()) {
                map.add(map1.getKey(), null);
            } else {
                map.add(map1.getKey(), map1.getValue());
            }
        }

        HttpEntity> request = new HttpEntity<>(map, headers);
        try {
            ResponseEntity response = restTemplate.postForEntity(url, request, responseType);
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ConnectionException("无响应或者响应结果异常");
        }
    }

    public T postUploadFile(String url, HttpHeaders headers, Map params, FileSystemResource fileSystemResource,Class responseType) {
        HttpEntity httpEntity = new HttpEntity<>(fileSystemResource, headers);
        try {
            ResponseEntity response = restTemplate.postForEntity(url, httpEntity, responseType, params);
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ConnectionException("无响应或者响应结果异常");
        }
    }

    public  E postForEntity(String url, T t, Class target) {
        HttpHeaders headers = new HttpHeaders();
        //设置类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        return postForEntity(url, t, target, headers);
    }

    public  E postForEntity(String url, T t, Class target, HttpHeaders headers) {
        HttpEntity formEntity = new HttpEntity<>(JSONObject.toJSON(t), headers);
        try {
            //发送数据方法
            ResponseEntity forEntity = restTemplate.postForEntity(url, formEntity, target);
            return forEntity.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ConnectionException("无响应或者响应结果异常");
        }
    }

    public  T getForEntity(String url, Map parameters, Class target) {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        return getForEntity(url, headers, parameters, target);
    }

    public  T getForEntity(String url, Class target) {
        try {
            return restTemplate.getForEntity(url, target).getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ConnectionException("无响应或者响应结果异常");
        }
    }

    public  T getForEntity(String url, Map headers, Map parameters, Class target) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll(headers);
        return getForEntity(url, headers, parameters, target);
    }

    public  T getForEntity(String url, HttpHeaders headers, Map parameters, Class target) {
        HttpEntity formEntity = new HttpEntity(headers);
        try {
            //发送数据方法
            ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, formEntity, target, parameters);
            return responseEntity.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ConnectionException("无响应或者响应结果异常");
        }
    }

    public  T header(String url, HttpMethod httpMethod, Map headerMap, Class target) {
        return restTemplate.exchange(url, httpMethod, getHttpEntity(headerMap), target).getBody();
    }

    private static HttpEntity getHttpEntity(Map map) {
        HttpHeaders headers = new HttpHeaders();
        for (Map.Entry map1 : map.entrySet()) {
            headers.add(map1.getKey(), map1.getValue().toString());
        }
        return new HttpEntity(null, headers);
    }
}