当前位置:网站首页>New load balancing webclient CRUD
New load balancing webclient CRUD
2022-06-22 15:43:00 【InfoQ】
Webclient Use scenarios
Webclient Of RestFul request
One 、RESTful Style and HTTP method
POST
- block() Block the method to get the response result
- subscribe() Non blocking asynchronous result subscription method
- retrieve() obtain HTTP Response body ,exchange() In addition to getting HTTP Response body , You can also get HTTP Status code 、headers、cookies etc. HTTP Message information .
- Use Mono Receive the response result of a single object , Use Flux Receive the response result of the collection class object .
- Placeholder syntax parameter passing method
Submit data for simulation form
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());
}
retrieveblockTransfer objects to JSON Send as data
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_JSONSimulate sending... To the server JSON String data
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);
// Output results
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 Number of set elements :" + li.size());
}
边栏推荐
- 得物技术复杂 C 端项目的重构实践
- FPGA采集DHT11温湿度
- keil MDK 中使用虚拟串口调试串口
- Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
- Once, I had 5 part-time jobs just to buy a new earring for my girlfriend
- 【newman】postman生成漂亮的测试报告
- 高精度计算
- At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source
- [graduation project] Research on emotion analysis based on semi supervised learning and integrated learning
- 2022年失业的人多吗?今年是不是特别难找工作?
猜你喜欢

Good wind relies on strength – code conversion practice of accelerating SQL Server Migration with babelfish

Tree structured binary tree

C语言学习-18-makefile文件编写例子以及如何生成、调用动态库

多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中

FPGA collects DHT11 temperature and humidity

At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source

那些没考上大学的人,后来过的怎样

How to use the concat() function of MySQL

建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!

I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!
随机推荐
又可以这样搞nlp(分类)
Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
"Software defines the world, open source builds the future" 2022 open atom global open source summit will open at the end of July
标准化、最值归一化、均值归一化应用场景的进阶思考
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
Recommendation of individual visa free payment scheme
mysql如何修改存储引擎为innodb
山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
How to open an account in flush? Is online account opening safe?
蓝桥杯2019年国赛最长子序列
I took a private job and earned 15250. Is it still necessary to do my main business?
社区文章|MOSN 构建 Subset 优化思路分享
DevSecOps: CI/CD 流水线安全的最佳实践
Please, don't be brainwashed. This is the living reality of 90% of Chinese people
On the routing tree of gin
Bochs software usage record
【newman】postman生成漂亮的测试报告
C# 实现插入排序
keil MDK 中使用虚拟串口调试串口