WebClientUtil.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.njuzr.eaibackend.utils;
  2. import io.netty.handler.ssl.SslContextBuilder;
  3. import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.client.reactive.ReactorClientHttpConnector;
  9. import org.springframework.web.reactive.function.client.WebClient;
  10. import org.springframework.web.reactive.function.client.WebClientResponseException;
  11. import reactor.netty.http.client.HttpClient;
  12. import reactor.netty.transport.ProxyProvider;
  13. import java.nio.file.Path;
  14. import java.util.HashMap;
  15. import java.util.function.Consumer;
  16. /**
  17. * @author: Leonezhurui
  18. * @Date: 2024/2/25 - 16:52
  19. * @Package: EAI-Backend
  20. */
  21. @Slf4j
  22. public class WebClientUtil {
  23. @Value("${proxy.enable}")
  24. private Boolean enableProxy;
  25. @Value("${proxy.host}")
  26. private String proxyHost;
  27. @Value("${proxy.port}")
  28. private int proxyPort;
  29. private final WebClient webClient;
  30. public WebClientUtil() {
  31. this.webClient = WebClient.builder().build();
  32. }
  33. // 构造函数,使用baseUrl初始化WebClient
  34. public WebClientUtil(String baseUrl) {
  35. // SslContextBuilder sslContextBuilder = SslContextBuilder
  36. // .forClient()
  37. // .trustManager(InsecureTrustManagerFactory.INSTANCE); // 信任所有证书
  38. //
  39. // HttpClient httpClient = HttpClient.create()
  40. // .secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
  41. // this.webClient = WebClient.builder()
  42. // .clientConnector(new ReactorClientHttpConnector(httpClient))
  43. // .baseUrl(baseUrl)
  44. // .build();
  45. if (enableProxy) {
  46. HttpClient httpClient = HttpClient.create()
  47. .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
  48. .host(proxyHost)
  49. .port(proxyPort));
  50. this.webClient = WebClient.builder()
  51. .clientConnector(new ReactorClientHttpConnector(httpClient))
  52. .baseUrl(baseUrl)
  53. .build();
  54. } else {
  55. this.webClient = WebClient.builder()
  56. .baseUrl(baseUrl)
  57. .build();
  58. }
  59. }
  60. // GET请求方法
  61. public <T> T get(String uri, Class<T> responseType) {
  62. try {
  63. return this.webClient.get()
  64. .uri(uri)
  65. .retrieve()
  66. .bodyToMono(responseType)
  67. .block(); // 转换为阻塞调用
  68. } catch (WebClientResponseException e) {
  69. log.error("WebClient发送GET请求失败:" + e.getMessage());
  70. throw new RuntimeException("Failed to get response: " + e.getMessage(), e);
  71. }
  72. }
  73. // POST请求方法
  74. public <T, R> T post(String uri, R request, Class<T> responseType) {
  75. try {
  76. return this.webClient.post()
  77. .uri(uri)
  78. .bodyValue(request)
  79. .retrieve()
  80. .bodyToMono(responseType)
  81. .block(); // 转换为阻塞调用
  82. } catch (WebClientResponseException e) {
  83. log.error("WebClient发送POST请求失败:" + e.getMessage());
  84. throw new RuntimeException("Failed to post data: " + e.getMessage(), e);
  85. }
  86. }
  87. // PUT请求方法
  88. public <T, R> T put(String uri, R request, Class<T> responseType) {
  89. try {
  90. return this.webClient.put()
  91. .uri(uri)
  92. .bodyValue(request)
  93. .retrieve()
  94. .bodyToMono(responseType)
  95. .block(); // 转换为阻塞调用
  96. } catch (WebClientResponseException e) {
  97. log.error("WebClient发送PUT请求失败:" + e.getMessage());
  98. throw new RuntimeException("Failed to put data: " + e.getMessage(), e);
  99. }
  100. }
  101. // DELETE请求方法
  102. public <T> T delete(String uri, Class<T> responseType) {
  103. try {
  104. return this.webClient.delete()
  105. .uri(uri)
  106. .retrieve()
  107. .bodyToMono(responseType)
  108. .block(); // 转换为阻塞调用
  109. } catch (WebClientResponseException e) {
  110. log.error("WebClient发送DELETE请求失败:" + e.getMessage());
  111. throw new RuntimeException("Failed to delete resource: " + e.getMessage(), e);
  112. }
  113. }
  114. public <T, R> T postWithToken(String uri, R request, Class<T> responseType, String key) {
  115. try {
  116. return this.webClient.post()
  117. .uri(uri)
  118. .header("Authorization", "Bearer " + key)
  119. .bodyValue(request)
  120. .retrieve()
  121. .bodyToMono(responseType)
  122. .block(); // 转换为阻塞调用
  123. } catch (WebClientResponseException e) {
  124. log.error("WebClient发送POST请求失败:" + e.getMessage());
  125. throw new RuntimeException("Failed to post data: " + e.getMessage(), e);
  126. }
  127. }
  128. }