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调用)的更多相关文章

  1. SpringMVC实现PUT请求上传文件

    在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体 ...

  2. Postman Post请求上传文件

    Postman Post请求上传文件一.选择post请求方式,输入请求地址 二.填写Headers Key:Content-Type :Value:multipart/form-data 如下图 三. ...

  3. SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)

    1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...

  4. python中使用multipart/form-data请求上传文件

    最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: { "salar ...

  5. element-ui上传组件,通过自定义请求上传文件

    记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...

  6. python发送post请求上传文件,无法解析上传的文件

    前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...

  7. Java客户端通过Http发送POST请求上传文件到web服务器

    http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html 1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.n ...

  8. JAVA模拟HTTP post请求上传文件

    在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...

  9. 通过PHP CURL模拟请求上传文件|图片。

    现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...

  10. 通过POST请求上传文件

    转自:https://blog.csdn.net/zhangge3663/article/details/81218488 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交 ...

随机推荐

  1. three.js 添加html内容、文本

    需求: 1.在场景内添加html元素并动态更新 2.html内容需跟随场景变化 方案: 新加方案:https://www.zhihu.com/question/49929467/answer/1186 ...

  2. python3.7+flask+alipay 支付宝付款功能

    文档参考github:https://github.com/fzlee/alipay/blob/master/docs/init.md 沙箱环境配置:https://opendocs.alipay.c ...

  3. P6628-[省选联考 2020 B 卷] 丁香之路【欧拉回路,最小生成树】

    正题 题目链接:https://www.luogu.com.cn/problem/P6628 题目大意 给出\(n\)个点的一张完全无向图,\(i\sim j\)的边权是\(|i-j|\). 然后给出 ...

  4. GDOI2021划水记

    Day0 上午有意志行,一大早就醒了,然后走了五个小时脚痛.中午洗澡,宿舍轮流看巨人最终话然后聊了一个小时? 下午老师带着我和全爷先开溜,宿舍好像很破旧还还没得充电,领了牌牌和斐爷去吃饭. 然后六点多 ...

  5. 解决报错:Unable to process Jar entry [org/springframework/jmx/export/annotation/*****]

    情况说明:从gitub上clone的maven项目,pox.xml配置中的依赖,自己的repository都有,所以正常update project ,正常clean,install,整个过程无报错 ...

  6. 在你面前有一个n阶的楼梯,你一步只能上1阶或2阶。 请问,当N=11时,你可以采用多少种不同的方式爬完这个楼梯();当N=9时呢?

    在你面前有一个n阶的楼梯,你一步只能上1阶或2阶.请问,当N=11时,你可以采用多少种不同的方式爬完这个楼梯:当N=9时呢? 思路解析 ①台阶只有一级阶梯时,只有一种走法. ②当台阶有两级时,可以先走 ...

  7. C#开发BIMFACE系列44 服务端API之计算图纸对比差异项来源自哪个图框

    BIMFACE二次开发系列目录     [已更新最新开发文章,点击查看详细] 在前两篇博客<C#开发BIMFACE系列42 服务端API之图纸对比>.<C#开发BIMFACE系列43 ...

  8. 题解 「BJOI2018 治疗之雨」

    题目传送门 题目大意 有一个初始为 \(p\) 的数,每次操作分为以下两个: 有 \(\frac{1}{m+1}\) 的概率$+1,但是中途 \(p\) 的最大值只能为 \(n\)$ 有 \(k\) ...

  9. MyBatis 中两表关联查询MYSQL (14)

    MyBatis 中两表关联查询MYSQL 1.创建数据库表语句 2.插入测试数据 3.pom文件内容 <?xml version="1.0" encoding="U ...

  10. Java(11)方法详细介绍

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201577.html 博客主页:https://www.cnblogs.com/testero ...