过滤器
WebClient.builder().baseUrl("https://jsonplaceholder.typicode.com/posts")
.filter((request, next) -> {
ClientRequest clientRequest = ClientRequest.from(request).header("foo", "bar").build();
System.out.println("请求方式 " + clientRequest.method() + "请求 URL" + clientRequest.url() + "请求路径");
return next.exchange(clientRequest);
})
.build().get().uri("/1")
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(String.class)).subscribe(System.out::println);WebClient.builder().baseUrl("https://jsonplaceholder.typicode.com/posts")
.filter((request, next) -> {
ClientRequest clientRequest = ClientRequest.from(request).header("foo", "bar").build();
System.out.println("请求方式 " + clientRequest.method() + "请求 URL" + clientRequest.url() + "请求路径");
return next.exchange(clientRequest).flatMap(clientResponse -> {
if (clientResponse.statusCode().value() == HttpStatus.UNAUTHORIZED.value()) {
return clientResponse.releaseBody().then(Mono.error(new RuntimeException("Unauthorized")));
} else {
return Mono.just(clientResponse);
}
});
})
.build().get().uri("/1")
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(String.class)).subscribe(System.out::println);最后更新于