当前位置:网站首页>新版负载均衡WebClient CRUD
新版负载均衡WebClient CRUD
2022-06-22 14:16:00 【InfoQ】
Webclient 使用场景
Webclient 的RestFul 请求
一、RESTful风格与HTTP method
POST
- block()阻塞获取响应结果的方法
- subscribe()非阻塞异步结果订阅方法
- retrieve()获取HTTP响应体,exchange()除了获取HTTP响应体,还可以获取HTTP 状态码、headers、cookies等HTTP报文信息。
- 使用Mono接收单个对象的响应结果,使用Flux接收集合类对象的响应结果。
- 占位符语法传参方式
模拟表单提交数据
public void testFormSubmit() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", "damoin");
map.add("UID", "11024319902323");
Mono<String> mono = webClientBuilder.build().post()
.uri("http://rest-service-service/add")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData(map))
.retrieve()
.bodyToMono(String.class);
System.out.println(mono.block());
}
retrieveblock传输对象以JSON数据形式发送
public void testPostJson() {
SysUser user = new SysUser();
user.setRealName("dwdwdww");
user.setPhone("32323232");
Mono<String> mono = webClientBuilder.build()
.post()
.uri("http://rest-service-service/add")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(user)
.retrieve()
.bodyToMono(String.class);
System.out.println(mono.block());
}
MediaType.APPLICATION_JSON模拟向服务端发送JSON字符串数据
public void testPostJsonStr() {
String jsonStr = "{\"realName\": \"damon\",\"phone\": \"32323232\"}";
Mono<String> mono = webClientBuilder.build().post()
.uri("http://rest-service-service/add")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(jsonStr))
.retrieve()
.bodyToMono(String.class);
// 输出结果
System.out.println(mono.block());
}
MediaType.APPLICATION_JSONDELETE
public void testDelete() {
webClientBuilder.build()
.delete()
.uri("http://rest-service-service/1");
}
PUT
public void testPut() {
SysUser user = new SysUser();
user.setRealName("dwdwdww");
user.setPhone("32323232");
Mono<String> mono = webClientBuilder.build()
.put()
.uri("http://rest-service-service/1")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(user).retrieve().bodyToMono(String.class);
System.out.println(mono.block());
}
GET
@GetMapping(value = "/getClientResByWebClient2", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Mono<String> getClientResByWebClient2() throws Exception {
Mono<String> resp = webClientBuilder.build()
.get()
.uri("http://diff-ns-service-service/all/getService")
.retrieve().bodyToMono(String.class);
//.exchange().flatMap(clientResp -> clientResp.bodyToMono(String.class));
resp.subscribe(body -> System.out.println(body));
return resp;
}
public void testFlux() {
Flux<SysUser> flux = webClientBuilder.build()
.get()
.uri("http://diff-ns-service-service/all")
.retrieve()
.bodyToFlux(SysUser.class);
List<SysUser> li = flux.collectList().block();
assert li != null;
System.out.println("li集合元素数量:" + li.size());
}
边栏推荐
- 宏源期货开户安全么?宏源期货公司可以降低手续费?
- Can Google bidding account detect the global market?
- Reconstruction practice of complex C-end project of acquisition technology
- 问一下想获取到sqlserver的start_lsn有好的办法吗?
- 『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
- Charles 乱码问题解决
- 时隔17年,刘亦菲再次刷屏式爆红:普通人不想被淘汰,也要懂得这件事
- 极致效率,云原生数据库TDSQL-C安身立命的根本
- Please, don't be brainwashed. This is the living reality of 90% of Chinese people
- Database connection pool: stress testing
猜你喜欢
![[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide

What is the value of a website? Why build an independent station

FPGA collects DHT11 temperature and humidity

FreeRTOS task priority and interrupt priority
![[Software Engineering] planning and project management](/img/93/4b5b5034fbfb76adef1b4fe93a85cb.png)
[Software Engineering] planning and project management

润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东

网站存在的价值是什么?为什么要搭建独立站

【浙江大学】考研初试复试资料分享

Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world

2022年失业的人多吗?今年是不是特别难找工作?
随机推荐
How to use the concat() function of MySQL
鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
OpenVINO CPU加速调研
Zhongshanshan: engineers after being blasted will take off | ONEFLOW u
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
Database connection pool: stress testing
What are the five characteristics of network security? What are the five attributes?
加密市场进入寒冬,是“天灾”还是“人祸”?
Fast and accurate point cloud registration based on minimizing 3D NDT distance
U++编程 移动 学习笔记
RealNetworks vs. Microsoft: the battle in the early streaming media industry
天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
Method of using inout signal in Verilog
PowerPoint 教程,如何在 PowerPoint 中添加水印?
数据库连接池:压力测试
Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
Software architecture
历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
难怪考研热度这么高,这些是研究生才有的“隐藏福利”!