1、安装依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
2、测试代码
@GetMapping("/webclientGet")
public String webclientGet(){
WebClient webClient = WebClient.create();
Mono<String> mono = webClient
.method(HttpMethod.GET) // GET 请求
.uri("http://localhost:8001/dubboTest") // 请求路径
.retrieve() // 获取响应体
.bodyToMono(String.class); //响应数据类型转换
return mono.block();
}
@PostMapping("/webclientPost")
public Map<String,String> webclientPost(@RequestBody Map<String,String> params){
WebClient webClient = WebClient.create();
Mono<Map> mono=webClient
.method(HttpMethod.POST)
.uri("http://localhost:8001/bodyTest")
.bodyValue(params)
.retrieve()
.bodyToMono(Map.class);
return mono.block();
}
@PostMapping("/webclientPostA1")
public Map<String,String> webclientPostA1(@RequestBody Map<String,String> params){
WebClient webClient = WebClient.create();
Mono<Map> mono=webClient
.method(HttpMethod.POST)
.uri("http://localhost:8001/bodyTest")
.body(Mono.just(params),Map.class)
.retrieve()
.bodyToMono(Map.class);
return mono.block();
}
//删除
@DeleteMapping("/webclientDelete/{name}")
public String webclientDelete(@PathVariable("name") String name){
WebClient webClient = WebClient.create();
Mono<String> mono=webClient
.method(HttpMethod.DELETE)
.uri("http://localhost:8001/bodyTestDel/{name}",name)
.retrieve()
.bodyToMono(String.class);
return mono.block();
}