SpringBoot使用WebClient发送请求

前言

Spring 框架中,常用的发送 Http、Https请求的有 RestTemplateWebFlux中的WebClient
本文主要说一下 WebClient的基本使用。
主要原因及场景如下:

在一个类似监听机器的项目中,需要发送警告到某群聊。
这种警告可能会堆积,需要异步发送出去。

官方文档如下:
https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/htmlsingle/#boot-features-webflux
中文版如下:
https://www.cnblogs.com/jmcui/p/11775217.html

在SpringBoot中使用

1、引入依赖

        <!-- webclient config-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2、添加配置类

package com.huice.order.ops.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.codec.ClientCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.util.concurrent.TimeUnit;

/**
 * 配置 WebFlux,配置WebClient。
 *
 * @date: 2021/11/2 15:45
 * @see WebClient
 */
@Configuration
public class WebClientConfig {
    /**
     * URLConnection's connect timeout (in milliseconds).
     */
    @Value("${http.client.connect-timeout:3000}")
    Integer connectTimeout;

    /**
     * URLConnection's read timeout (in milliseconds).
     */
    @Value("${http.client.read-timeout:3000}")
    Integer readTimeout;


    /**
     * URLConnection's write timeout (in milliseconds).
     */
    @Value("${http.client.write-timeout:3000}")
    Integer writeTimeout;

    /**
     * 注入 WebClient
     *
     * @param objectMapper ObjectMapper实例
     * @return WebClient 实例
     * @see WebClient
     */
    @Bean("webClient")
    public WebClient webClient(ObjectMapper objectMapper) {
        HttpClient httpClient = HttpClient.create().tcpConfiguration(client -> client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout)
                .doOnConnected(
                        conn -> conn.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS))
                                .addHandlerLast(new WriteTimeoutHandler(writeTimeout, TimeUnit.MILLISECONDS))));
        ExchangeStrategies strategies = ExchangeStrategies
                .builder()
                .codecs((ClientCodecConfigurer clientDefaultCodecsConfigurer) -> {
                    clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON));
                    clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON));
                }).build();
        return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).exchangeStrategies(strategies).build();
    }

    @Bean
    public ObjectMapper objectMapper(){
        return new ObjectMapper();
    }
}

3、使用

	// 注入 webclient
	@Resource
    private WebClient webClient;
	
	@Value("请求的url")
	private String url;
    // 请求体
    Map<String, Object> params = new HashMap<>(16);
    // webclient发送请求
    webClient.method(HttpMethod.POST).uri(url).bodyValue(params).retrieve()
    		// 转换返回结果:这个可以自己定义,需要有空构造器的bean即可。
            .bodyToMono(String.class)
            // 请求成功后的响应
            .doOnNext(resp -> log.debug("发送告警消息 [{}]", resp))
            // 发生错误
            .doOnError(throwable -> log.error(throwable.getLocalizedMessage()))
            // 异步消费;同步时使用blockOptional()即可
            .subscribe();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你家宝宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值