WebClient发送请求公共方法

阅读更多
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 post(Object paramter, String url, Class resultType) {

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

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

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

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

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

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

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

    /**
     *
     * @param url
     *            请求路径
     * @param header
     *            请求头
     * @param resultType
     *            返回结果类型
     * @return
     */
    public static  V get(String url, Map header, Class 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 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')

 

你可能感兴趣的:(Spring5,WebClient,发送请求,公共方法)