Spring 之 RestTemplate VS WebClient 发送 HTTP请求
WebClient 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
| WebClient 方法 | 注解 |
|---|---|
| block() | 阻塞式获取响应结果 |
| subscribe() | 非阻塞式获取响应结果 |
| exchange() | 获取HTTP响应完整内容:HTTP报文中的状态码、headers等信息 |
| retrieve() | 只能获取HTTP报文中的Body |
WebClient Post请求发送JSON字符串、对象、表单数据
RestTemplate Post请求发送JSON字符串、对象、表单数据
@Test
void contextLoads3() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
//设置请求头
HttpHeaders headers = new HttpHeaders();
//设置参数
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap();
body.add("tel", "15333065057");
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity(body, headers);
//请求接口,并接收返回的信息
String s = restTemplate.exchange("http://jsonplaceholder.typicode.com/posts", HttpMethod.POST, entity, String.class).getBody();
System.out.println(s);
}
Spring 之 RestTemplate VS WebClient 发送 HTTP请求的更多相关文章
- Web Server 使用WebClient 发送https请求 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
使用WebClient 发送https请求 使用WebClient发送请求时,并且是以https协议: WebClient webClient = new WebClient(); string re ...
- SpringBoot26 RestTemplate、WebClient
1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...
- JavaWeb 发送post请求的2种方式(form、json)
JavaWeb 发送post请求的2种方式(form.json) CreationTime--2018年6月20日10点15分 Author:Marydon 前提:通过HttpClient来实现 ...
- Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...
- 使用RestTemplate发送post请求
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- WebClient模拟发送Post请求
WebClient模拟发送Post请求方法: /// <summary> /// 模拟post请求 /// </summary> /// <param name=&quo ...
- WebClient以POST方式发送Web请求
本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求 Cookies前后一致.其中application/x-www ...
- 使用RestTemplate发送post请求,请求头中封装参数
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败.中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- 【Java SpringBoot】RestTemplate使用postForObject发送请求,接收String类型的response时中文乱码
https://blog.csdn.net/qq_43758789/article/details/120576717 使用RestTemplate发送PostForObject的时候会出现乱码.这是 ...
- 还不知道spring的RestTemplate的妙用吗
为什么要使用RestTemplate? 随着微服务的广泛使用,在实际的开发中,客户端代码中调用RESTful接口也越来越常见.在系统的遗留代码中,你可能会看见有一些代码是使用HttpURLConnec ...
随机推荐
- 学习笔记--Java中方法递归调用
Java中方法递归调用 public class RecursionTest01{ public static void main(String[] args){ System.out.println ...
- c++17
std::variant<std::string,int,float>://作为联合体 using va = std::variant<std::string, int, float ...
- php 模型浏览器
docker安装 文档:https://github.com/SeleniumHQ/docker-selenium docker run -d -p 4444:4444 -p 7900:7900 -- ...
- C# 推荐一种开机自启动的方式
概述(Overview) 网上多数搜索结果以注册表设置为优先,这个方法需要管理员权限,实际工作中可能并不适用.这个方法是直接写到用户开机自启动目录里,系统开机会带着一起启动.(Most search ...
- linux环境部署搭建流程
linux环境部署搭建流程 1,ubuntu/centos系统安装 2,ip网络配置 3,路由和防火墙 4,时间同步服务器 5,ssh协议配置(Windows安装xshell/secureCRT) 6 ...
- 【UEditor】富文本编辑器 简单上手
富文本编辑器是一个使用前端组件渲染的文本编辑器 功能强大,交互友好,我们写评论,写文章,一些文本编辑的地方就会使用这种编辑器插件 另外在官方文档的下面还提到了我们这个插件的一些后端软件[指Java]要 ...
- 2024年是否是人形机器人的元年 —— 继OpenAI/Google/特斯拉之后黄仁勋也宣布NVIDIA公司的人形机器人项目 —— Groot项目 —— 英伟达公布了人形机器人项目 GR00T
相关: https://www.youtube.com/watch?v=bMIRhOXAjYk 参考: https://www.thepaper.cn/newsDetail_forward_26730 ...
- 中国版的huggingface——始智AI-wisemodel
新闻: 始智AI-wisemodel社区正式上线,目标打造中国版"HuggingFace" 首先要知道,AI火热了7,8年了,不论是国内还是国外一直有要搞模型分享的网站,但是这东西 ...
- 【转载】 梯度的直观理解_谈谈优化算法之一(动量法、Nesterov法、自然梯度法)
原文地址: https://blog.csdn.net/weixin_34613462/article/details/112333623 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA ...
- 陆吾AI智能机械狗的通讯控制
陆吾AI智能机械狗现在是蛮有名的了,在YouTube上比较火的一个东西了,和波士顿机器狗不同,波士顿机器狗价格昂贵主要原因是其定位于工业领域的机械狗因此采用的是工业级的硬件,但是如果我们采用的家用环境 ...