1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
2.调用API
public class WebClientExample {
private final WebClient webClient;
public WebClientExample() {
this.webClient = WebClient.builder()
.baseUrl("http://example.com/api")
.build();
}
public String sendData(Map<String, Object> dataMap) {
String msg = "";
Mono<ApiResultDTO> response = webClient.post()
.uri("/endpoint")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(dataMap)
.retrieve()
.bodyToMono(ApiResultDTO.class).timeout(Duration.of(60, ChronoUnit.SECONDS));
ApiResultDTO data = response.block();
if (data.isSuccess()) {
msg = "成功";
} else {
msg = "失败"+data.getMsg();
}
return msg;
}
}