欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WebClient发送请求公共方法

程序员文章站 2022-05-31 16:11:56
...
import java.util.Map;

import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
import reactor.core.publisher.Mono;

import javax.net.ssl.SSLException;

public class MyWebClitntUtil {
    private static final MediaType MEDIATYPE = MediaType.APPLICATION_JSON_UTF8;

    /**
     *
     * @param paramter
     *            请求参数
     * @param url
     *            请求路径
     * @param resultType
     *            返回结果类型
     * @return
     */
    public static <V> V post(Object paramter, String url, Class<V> resultType) {

        return post(uri(url, HttpMethod.POST), paramter, resultType);
    }

    /**
     *
     * @param paramter
     *            请求参数
     * @param url
     *            请求路径
     * @param header
     *            请求头
     * @param resultType
     *            返回结果类型
     * @return
     */
    public static <V> V post(Object paramter, String url, Map<String, String> header, Class<V> resultType) {

        RequestBodySpec uri = uri(url, HttpMethod.POST);
        addHeader(header, uri);
        return post(uri, paramter, resultType);
    }

    private static <V> V post(RequestBodySpec uri, Object paramter, Class<V> resultType) {

        return uri.contentType(MEDIATYPE).body(Mono.just(paramter), Object.class).retrieve().bodyToMono(resultType)
                .block();
    }

    /**
     *
     * @param url
     *            请求路径
     * @param resultType
     *            返回结果类型
     * @return
     */
    public static <V> V get(String url, Class<V> resultType) {

        return uri(url, HttpMethod.GET).retrieve().bodyToMono(resultType).block();
    }

    /**
     *
     * @param url
     *            请求路径
     * @param header
     *            请求头
     * @param resultType
     *            返回结果类型
     * @return
     */
    public static <V> V get(String url, Map<String, String> header, Class<V> resultType) {

        RequestBodySpec uri = uri(url, HttpMethod.GET);
        addHeader(header, uri);
        return uri.retrieve().bodyToMono(resultType).block();
    }

    private static RequestBodySpec uri(String url, HttpMethod method) {

        return createWebClient().method(method).uri(url);
    }

    private static void addHeader(Map<String, String> header, RequestBodySpec uri) {

        if (!CollectionUtils.isEmpty(header)) {
            header.forEach((name, value) -> uri.header(name, value));
        }
    }

    public static WebClient createWebClient()  {//已绕开https证书认证
        SslContext sslContext = null;
        try {
            sslContext = SslContextBuilder
                    .forClient()
                    .trustManager(InsecureTrustManagerFactory.INSTANCE)
                    .build();
        } catch (SSLException e) {
            e.printStackTrace();
        }

        SslContext finalSslContext = sslContext;
        ClientHttpConnector httpConnector = new ReactorClientHttpConnector(opt -> {
            opt.sslContext(finalSslContext);
        });
        return WebClient.builder().clientConnector(httpConnector).build();
    }
}

依赖:
compile('org.springframework:spring-webflux')
compile('io.projectreactor.ipc:reactor-netty')