restTemple发送请求、上传文件(@LoadBalanced微服务调用及url调用)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;

/**
 * 
 */
@Configuration
public class RestConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        //httpRequestFactory.setConnectionRequestTimeout(3000);
        httpRequestFactory.setConnectTimeout(30000);
        httpRequestFactory.setReadTimeout(300000);
        return new RestTemplate(httpRequestFactory);
        //return new RestTemplate();
    }

}
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.Map;

/**
 * 
 * 微服务调用通用类(也可用作普通http类用)
 */
public class RestTemplateUtils {


    /**
     * 发送get请求
     * @param url
     * @param params
     * @param headers
     * @param responseType
     * @param <T>
     * @return
     */
    public static <T> T doHttpGet(String url, Map<String, String> params, Map<String, String> headers, boolean isSpringCloud, Class<T> responseType) {
        RestTemplate restTemplate = getRestTemplate(isSpringCloud);
        HttpHeaders restHeaders = new HttpHeaders();
        restHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        restHeaders.add(CloudCodeUtils.WenToken, CloudCodeUtils.createCode());

        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                restHeaders.add(entry.getKey(), entry.getValue());
            }
        }

        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (url.contains("?")) {
                    url = url + "&" + entry.getKey() + "=" + entry.getValue();
                } else {
                    url = url + "?" + entry.getKey() + "=" + entry.getValue();
                }
            }
        }
        HttpEntity<String> restRequest = new HttpEntity<>(null, restHeaders);

        ResponseEntity<T> result = restTemplate.exchange(url, HttpMethod.GET, restRequest, responseType);

        return result.getBody();
        //return restTemplate.getForObject(url, responseType, restRequest);
    }

    /**
     * 发送post请求
     * @param url
     * @param params
     * @param headers
     * @param responseType
     * @param <T>
     * @return
     */
    public static <T> T doHttpPost(String url, Map<String, String> params, Map<String, String> headers, boolean isSpringCloud, Class<T> responseType) {
        RestTemplate restTemplate = getRestTemplate(isSpringCloud);
        HttpHeaders restHeaders = new HttpHeaders();
        MultiValueMap<String, Object> restParam = new LinkedMultiValueMap<>();

        restHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        restHeaders.add(CloudCodeUtils.WenToken, CloudCodeUtils.createCode());

        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                restHeaders.add(entry.getKey(), entry.getValue());
            }
        }

        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                restParam.add(entry.getKey(), entry.getValue());
            }
        }

        HttpEntity<MultiValueMap<String, Object>> restRequest = new HttpEntity<>(restParam, restHeaders);
        return restTemplate.postForObject(url, restRequest, responseType);
    }


    /**
     * 微服务url格式 String url = "http://服务名/provider/xxx";
     * 上传文件
     * @param url
     * @param params
     * @param files
     * @param headers
     * @param responseType
     * @param <T>
     * @return
     */
    public static <T> T doUploadFile(String url, Map<String, String> params, Map<String, File> files, Map<String, String> headers, boolean isSpringCloud, Class<T> responseType) {
        RestTemplate restTemplate = getRestTemplate(isSpringCloud);
        HttpHeaders restHeaders = new HttpHeaders();
        MultiValueMap<String, Object> restParam = new LinkedMultiValueMap<>();

        restHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        restHeaders.add(CloudCodeUtils.WenToken, CloudCodeUtils.createCode());

        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                restHeaders.add(entry.getKey(), entry.getValue());
            }
        }

        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                restParam.add(entry.getKey(), entry.getValue());
            }
        }

        if (files != null) {
            for (Map.Entry<String, File> entry : files.entrySet()) {
                restParam.add(entry.getKey(), new FileSystemResource(entry.getValue()));
            }
        }
        HttpEntity<MultiValueMap<String, Object>> restRequest = new HttpEntity<>(restParam, restHeaders);
        return restTemplate.postForObject(url, restRequest, responseType);
    }

    /**
     * 获取RestTemplate
     * @param isSpringCloud
     * @return
     */
    public static RestTemplate getRestTemplate(boolean isSpringCloud){
        RestTemplate restTemplate = null;
        if(isSpringCloud){
            restTemplate = SpringContextHolder.getBean(RestTemplate.class);
            /*HttpComponentsClientHttpRequestFactory httpRequestFactory = ((HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory());
            httpRequestFactory.setConnectTimeout(3000);
            httpRequestFactory.setReadTimeout(300000);*/
        }else{
            HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
            httpRequestFactory.setConnectTimeout(30000);
            httpRequestFactory.setReadTimeout(60000);
            restTemplate = new RestTemplate(httpRequestFactory);
        }
        return restTemplate;
    }



}

 

restTemple发送请求、上传文件(@LoadBalanced微服务调用及url调用)

上一篇:一款非常简单的android音乐播放器源码分享给大家


下一篇:oceanbase数据库开启回收站功能