245 字
1 分钟
Spring之WebClient

参考链接#

URI Links——Spring/WebServlet

WebClient——Spring/Web on Reactive Stack

玩转Spring全家桶——极客时间@丁雪丰  

概述#

WebClient包含在SpringWebflux中。SpringMVC和SpringWebFlux都是MVC框架,SpringWebFlux从官方文档的说明上是一种非阻塞式栈web框架。

//实例创建
WebClient.create();
WebClient.builder();
//发起请求
get();
post();
put();
delete();
patch();
//基本用法
//获得结果
retrieve();
exchange();
//处理Http Status
onStatus();
//应答正文
bodyToMono();
bodyToFlux();

Getting Started#

package geektime.spring.reactor.webclient;
import geektime.spring.reactor.webclient.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.util.concurrent.CountDownLatch;
@SpringBootApplication
@Slf4j
public class WebclientDemoApplication implements ApplicationRunner {
@Autowired
private WebClient webClient;
public static void main(String[] args) {
new SpringApplicationBuilder(WebclientDemoApplication.class)
.web(WebApplicationType.NONE)
.bannerMode(Banner.Mode.OFF)
.run(args);
}
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder.baseUrl("http://localhost:8080").build();
}
@Override
public void run(ApplicationArguments args) throws Exception {
CountDownLatch cdl = new CountDownLatch(2);
webClient.get()
.uri("/coffee/{id}", 1)
.accept(MediaType.APPLICATION_JSON_UTF8)
.retrieve()
.bodyToMono(Coffee.class)
.doOnError(t -> log.error("Error: ", t))
.doFinally(s -> cdl.countDown())
.subscribeOn(Schedulers.single())
.subscribe(c -> log.info("Coffee 1: {}", c));
Mono<Coffee> americano = Mono.just(
Coffee.builder()
.name("americano")
.price(Money.of(CurrencyUnit.of("CNY"), 25.00))
.build()
);
webClient.post()
.uri("/coffee/")
.body(americano, Coffee.class)
.retrieve()
.bodyToMono(Coffee.class)
.doFinally(s -> cdl.countDown())
.subscribeOn(Schedulers.single())
.subscribe(c -> log.info("Coffee Created: {}", c));
cdl.await();
webClient.get()
.uri("/coffee/")
.retrieve()
.bodyToFlux(Coffee.class)
.toStream()
.forEach(c -> log.info("Coffee in List: {}", c));
}
}
Spring之WebClient
https://iszengmh.pages.dev/posts/spring之webclient/
作者
Rise Zeng
发布于
2019-08-30
许可协议
CC BY-NC-SA 4.0