响应式发送请求API(WebClinet)使用案例

package luck.spring.boot.webflux;

import cn.hutool.core.thread.ThreadUtil;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.netty.http.client.HttpClient;

import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;

@Slf4j
public class WebClientDemo {
    public static void main(String[] args) {
        // String res = HttpUtil.get("http://localhost:8080");
        // System.out.println(res);
        WebClient client = WebClient.create("http://localhost:8080");
        client.get()
                .uri("/router/cc")
                .accept(APPLICATION_JSON)
                .httpRequest(req -> {
                    log.error("request url {}", req.getURI().getPath());
                })
                .retrieve()
                .bodyToMono(String.class)
                .subscribeOn(Schedulers.boundedElastic())
                .doOnError(e -> {
                    log.error("error", e);
                })
                .subscribe(System.out::println);
        ThreadUtil.sleep(100000);

    }

    public static void test(String[] args) {
        // 构建一个发送请求的客户端,它就是HttpClient,HttpUtil,发送请求的
        WebClient client = WebClient.builder()
                // 自定义处理请求响应逻辑,是clientConnector+exchangeStrategies的替代方案
                // exchangeFunction完全自己处理请求逻辑,就不会走默认的处理逻
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值