| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package com.njuzr.eaibackend.utils;
- import io.netty.handler.ssl.SslContextBuilder;
- import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.http.client.reactive.ReactorClientHttpConnector;
- import org.springframework.web.reactive.function.client.WebClient;
- import org.springframework.web.reactive.function.client.WebClientResponseException;
- import reactor.netty.http.client.HttpClient;
- import reactor.netty.transport.ProxyProvider;
- import java.nio.file.Path;
- import java.util.HashMap;
- import java.util.function.Consumer;
- /**
- * @author: Leonezhurui
- * @Date: 2024/2/25 - 16:52
- * @Package: EAI-Backend
- */
- @Slf4j
- public class WebClientUtil {
- private final WebClient webClient;
- public WebClientUtil() {
- this.webClient = WebClient.builder().build();
- }
- // 构造函数,使用baseUrl初始化WebClient
- public WebClientUtil(String baseUrl) {
- // SslContextBuilder sslContextBuilder = SslContextBuilder
- // .forClient()
- // .trustManager(InsecureTrustManagerFactory.INSTANCE); // 信任所有证书
- //
- // HttpClient httpClient = HttpClient.create()
- // .secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
- // this.webClient = WebClient.builder()
- // .clientConnector(new ReactorClientHttpConnector(httpClient))
- // .baseUrl(baseUrl)
- // .build();
- HttpClient httpClient = HttpClient.create()
- .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
- .host("127.0.0.1")
- .port(7890));
- this.webClient = WebClient.builder()
- .clientConnector(new ReactorClientHttpConnector(httpClient))
- .baseUrl(baseUrl)
- .build();
- }
- // GET请求方法
- public <T> T get(String uri, Class<T> responseType) {
- try {
- return this.webClient.get()
- .uri(uri)
- .retrieve()
- .bodyToMono(responseType)
- .block(); // 转换为阻塞调用
- } catch (WebClientResponseException e) {
- log.error("WebClient发送GET请求失败:" + e.getMessage());
- throw new RuntimeException("Failed to get response: " + e.getMessage(), e);
- }
- }
- // POST请求方法
- public <T, R> T post(String uri, R request, Class<T> responseType) {
- try {
- return this.webClient.post()
- .uri(uri)
- .bodyValue(request)
- .retrieve()
- .bodyToMono(responseType)
- .block(); // 转换为阻塞调用
- } catch (WebClientResponseException e) {
- log.error("WebClient发送POST请求失败:" + e.getMessage());
- throw new RuntimeException("Failed to post data: " + e.getMessage(), e);
- }
- }
- // PUT请求方法
- public <T, R> T put(String uri, R request, Class<T> responseType) {
- try {
- return this.webClient.put()
- .uri(uri)
- .bodyValue(request)
- .retrieve()
- .bodyToMono(responseType)
- .block(); // 转换为阻塞调用
- } catch (WebClientResponseException e) {
- log.error("WebClient发送PUT请求失败:" + e.getMessage());
- throw new RuntimeException("Failed to put data: " + e.getMessage(), e);
- }
- }
- // DELETE请求方法
- public <T> T delete(String uri, Class<T> responseType) {
- try {
- return this.webClient.delete()
- .uri(uri)
- .retrieve()
- .bodyToMono(responseType)
- .block(); // 转换为阻塞调用
- } catch (WebClientResponseException e) {
- log.error("WebClient发送DELETE请求失败:" + e.getMessage());
- throw new RuntimeException("Failed to delete resource: " + e.getMessage(), e);
- }
- }
- public <T, R> T postWithToken(String uri, R request, Class<T> responseType, String key) {
- try {
- return this.webClient.post()
- .uri(uri)
- .header("Authorization", "Bearer " + key)
- .bodyValue(request)
- .retrieve()
- .bodyToMono(responseType)
- .block(); // 转换为阻塞调用
- } catch (WebClientResponseException e) {
- log.error("WebClient发送POST请求失败:" + e.getMessage());
- throw new RuntimeException("Failed to post data: " + e.getMessage(), e);
- }
- }
- }
|