当前位置:网站首页>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());
}
边栏推荐
- 那些没考上大学的人,后来过的怎样
- At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source
- Bochs software usage record
- C# 实现插入排序
- 好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
- C语言学生成绩排名系统
- FPGA采集DHT11温湿度
- "Software defines the world, open source builds the future" 2022 open atom global open source summit will open at the end of July
- Fast and accurate point cloud registration based on minimizing 3D NDT distance
- Show me my personal work list for the past two years. I earn 6K a month in my spare time. It's so delicious to have a sideline
猜你喜欢

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

FPGA collects DHT11 temperature and humidity

Development status of full color LED display

小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效

数据库连接池:压力测试

Please, don't be brainwashed. This is the living reality of 90% of Chinese people

mysql如何将字段修改为not null

Those confusing user state & kernel state

全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer

Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node
随机推荐
Verilog使用inout信号的方法
英国考虑基于国家安全因素让Arm在伦敦上市
On the routing tree of gin
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
Tdengine connector goes online Google Data Studio store
TDengine 连接器上线 Google Data Studio 应用商店
Mitsubishi manipulator demo program
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
数据库连接池:连接池功能点的实现
FreeRtos 任务优先级和中断优先级
问一下想获取到sqlserver的start_lsn有好的办法吗?
再次认识 WebAssembly
Sdvo:ldso+ semantics, direct French Slam (RAL 2022)
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
向量1(类和对象)
UE4 obtains local files through blueprints
mysql如何修改存储引擎为innodb
Can Google bidding account detect the global market?