ToB企服应用市场:ToB评测及商务社交产业平台

标题: SpringBoot3 实现webclient 通用方法 [打印本页]

作者: 八卦阵    时间: 2024-8-16 07:30
标题: SpringBoot3 实现webclient 通用方法
媒介:

Spring Boot WebClient 是 Spring Framework 5 中引入的一个新的响应式 Web 客户端,用于异步和响应式地与外部服务进行通信。它是基于 Project Reactor 的响应式编程模型构建的,提供了比传统的 RestTemplate 更现代和强盛的功能
先容:

一、引包

  1. <parent>
  2.         <groupId>org.springframework.boot</groupId>
  3.         <artifactId>spring-boot-starter-parent</artifactId>
  4.         <version>3.0.0</version>
  5.     </parent>
  6. <dependencies>
  7.         <dependency>
  8.             <groupId>org.springframework.boot</groupId>
  9.             <artifactId>spring-boot-starter-web</artifactId>
  10.         </dependency>
  11.         <dependency>
  12.             <groupId>org.springframework.boot</groupId>
  13.             <artifactId>spring-boot-starter-tomcat</artifactId>
  14.         </dependency>
  15.         <dependency>
  16.             <groupId>org.springframework.boot</groupId>
  17.             <artifactId>spring-boot-starter-test</artifactId>
  18.             <scope>test</scope>
  19.         </dependency>
  20.           <dependency>
  21.             <groupId>org.springframework.boot</groupId>
  22.             <artifactId>spring-boot-starter-webflux</artifactId>
  23.         </dependency>
  24. </dependencies>
复制代码
二、通用方法

  1. package com.zc.util;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.web.reactive.function.client.WebClient;
  6. import java.util.List;
  7. /**
  8. * @author zc
  9. * @date 2024/4/15 16:52
  10. * @desc
  11. */
  12. @Component
  13. public class WebClientUtil {
  14.     private final WebClient webClient;
  15.     @Autowired
  16.     public WebClientUtil(WebClient.Builder webClientBuilder) {
  17.          this.webClient = webClientBuilder.
  18.                 baseUrl("http://127.0.0.1:30003").
  19.                 build();
  20.     }
  21.     /**
  22.      * get方法
  23.      * @param url
  24.      * @param responseType
  25.      * @return
  26.      * @param
  27.      */
  28.     public <T> T get(String url, Class<T> responseType) {
  29.         return webClient.get().
  30.                 uri(url).
  31.                 accept(MediaType.APPLICATION_JSON).
  32.                 retrieve().
  33.                 bodyToMono(responseType).
  34.                 block();
  35.     }
  36.     /**
  37.      * get多条数据
  38.      * @param url
  39.      * @param responseType
  40.      * @return
  41.      * @param <T>
  42.      */
  43.     public <T> List<T> list(String url, Class<T> responseType){
  44.         return webClient.get().
  45.                 uri(url).
  46.                 accept(MediaType.APPLICATION_JSON).
  47.                 retrieve().
  48.                 bodyToFlux(responseType).collectList().block();
  49.     }
  50.     /**
  51.      * post方法
  52.      * @param url
  53.      * @param requestBody
  54.      * @param responseType
  55.      * @return
  56.      * @param
  57.      */
  58.     public <T> T post(String url, Object requestBody, Class<T> responseType) {
  59.         return webClient.post().
  60.                 uri(url).
  61.                 contentType(MediaType.APPLICATION_JSON).
  62.                 bodyValue(requestBody).
  63.                 accept(MediaType.APPLICATION_JSON).
  64.                 retrieve().
  65.                 bodyToMono(responseType).
  66.                 block();
  67.     }
  68.     /**
  69.      * put方法
  70.      * @param url
  71.      * @param requestBody
  72.      * @param responseType
  73.      * @return
  74.      * @param
  75.      */
  76.     public <T> T put(String url, Object requestBody, Class<T> responseType){
  77.         return webClient.put().
  78.                 uri(url).
  79.                 contentType(MediaType.APPLICATION_JSON).
  80.                 bodyValue(requestBody).
  81.                 accept(MediaType.APPLICATION_JSON).
  82.                 retrieve().
  83.                 bodyToMono(responseType).
  84.                 block();
  85.     }
  86.     /**
  87.      * 删除方法
  88.      * @param url
  89.      * @param responseType
  90.      * @return
  91.      * @param
  92.      */
  93.     public <T> T delete(String url, Class<T> responseType){
  94.         return webClient.delete().
  95.                 uri(url).
  96.                 accept(MediaType.APPLICATION_JSON).
  97.                 retrieve().
  98.                 bodyToMono(responseType).
  99.                 block();
  100.     }
  101. }
复制代码
三、测试

客户端:
  1. import com.alibaba.fastjson.JSON;
  2. import com.zc.Application;
  3. import com.zc.bean.HostDiffBean;
  4. import com.zc.util.WebClientUtil;
  5. import org.junit.jupiter.api.Test;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import java.util.Date;
  9. import java.util.List;
  10. /**
  11. * @author zc
  12. * @date 2024/2/23 10:40
  13. * @desc
  14. */
  15. @SpringBootTest(classes = Application.class)
  16. public class TestFF {
  17.     @Autowired
  18.     private WebClientUtil webClientUtil;
  19.     @Test
  20.     public void test(){
  21.         List<HostDiffBean> list= webClientUtil.list("compare/hostInfo?pageSize=10&pageNum=1", HostDiffBean.class);
  22.         System.out.println(JSON.toJSON(list));
  23.         HostDiffBean hostDiffBean = new HostDiffBean();
  24.         hostDiffBean.setIp("127.0.0.1");
  25.         hostDiffBean.setHIp("127.0.0.2");
  26.         hostDiffBean.setCreateTime(new Date());
  27.         hostDiffBean.setSerialNumber("123");
  28.         hostDiffBean.setHDeviceNo("no123");
  29.         hostDiffBean.setDeviceNo("NO456");
  30.         String result = webClientUtil.post("compare/hostInfo/add", hostDiffBean, String.class);
  31.         System.out.println(result);
  32.     }
  33. }
复制代码
服务端:
  1. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  2. import com.neusoft.bean.*;
  3. import com.neusoft.service.HostDataCompareService;
  4. import io.swagger.annotations.*;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.validation.annotation.Validated;
  7. import org.springframework.web.bind.annotation.*;
  8. import java.util.List;
  9. /**
  10. * @author zc
  11. * @date 2024/3/14 15:00
  12. * @desc
  13. */
  14. @RestController
  15. @RequestMapping("/compare")
  16. @Api(value = "数据对比接口", tags = "数据对比接口")
  17. public class DataCompareController {
  18.     @Autowired
  19.     private HostDataCompareService hostDataCompareService;
  20.     @GetMapping("/hostInfo")
  21.     @ApiOperation(value = "宿主机数量差异查询", notes = "宿主机数量差异查询")
  22.     public List<HostInfoBean> getHostInfoBeans(@RequestParam(name = "pageSize") @Validated @ApiParam(value = "每页数", required = true) Integer pageSize,
  23.                                                @RequestParam(name = "pageNum") @Validated @ApiParam(value = "页数", required = true) Integer pageNum) {
  24.         Page<HostInfoBean> list = hostDataCompareService.getHostInfoBeans(pageSize, pageNum);
  25.         return list.getRecords();
  26.     }
  27.     @PostMapping("/hostInfo/add")
  28.     @ApiOperation(value = "宿主机数量差异查询", notes = "宿主机数量差异查询")
  29.     public String addHostInfoBeans(@RequestBody HostDiffBean hostDiffBean){
  30.         return "success";
  31.     }
  32. }
复制代码
结果:


四、参考

SpringBoot - 网络哀求客户端WebClient使用详解_springboot webclient-CSDN博客

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4