过滤器

WebClient 也提供了 Filter,对应于 org.springframework.web.reactive.function.client.ExchangeFilterFunction 接口,其接口方法定义如下。

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);

最后更新于

这有帮助吗?