SpringBoot3實(shí)現(xiàn)webclient的通用方法詳解
前言
Spring Boot WebClient 是 Spring Framework 5 中引入的一個(gè)新的響應(yīng)式 Web 客戶端,用于異步和響應(yīng)式地與外部服務(wù)進(jìn)行通信。它是基于 Project Reactor 的響應(yīng)式編程模型構(gòu)建的,提供了比傳統(tǒng)的 RestTemplate 更現(xiàn)代和強(qiáng)大的功能
介紹
響應(yīng)式編程模型:WebClient 是基于響應(yīng)式編程模型的,這意味著它可以非阻塞地執(zhí)行網(wǎng)絡(luò)請(qǐng)求,并且能夠與流式數(shù)據(jù)交互。這使得 WebClient 在處理大量并發(fā)請(qǐng)求時(shí)具有更高的性能和可伸縮性。
異步操作:WebClient 支持異步操作,這意味著它可以在等待網(wǎng)絡(luò)響應(yīng)的同時(shí)繼續(xù)執(zhí)行其他任務(wù)。這有助于提高應(yīng)用程序的響應(yīng)能力和吞吐量。
強(qiáng)大的 API:WebClient 提供了一個(gè)簡(jiǎn)潔而強(qiáng)大的 API,用于構(gòu)建 HTTP 請(qǐng)求和接收響應(yīng)。它支持多種 HTTP 方法(如 GET、POST、PUT、DELETE 等),并提供了豐富的功能來處理請(qǐng)求頭、請(qǐng)求體、響應(yīng)體等。
流式處理:WebClient 支持流式處理響應(yīng)數(shù)據(jù),這意味著它可以在接收響應(yīng)數(shù)據(jù)的同時(shí)進(jìn)行處理,而不需要將整個(gè)響應(yīng)加載到內(nèi)存中。這有助于處理大型響應(yīng)數(shù)據(jù),并減少內(nèi)存使用。
錯(cuò)誤處理:WebClient 提供了強(qiáng)大的錯(cuò)誤處理機(jī)制,可以方便地處理網(wǎng)絡(luò)請(qǐng)求中出現(xiàn)的錯(cuò)誤和異常情況。它支持自定義錯(cuò)誤處理器,可以根據(jù)需要定義錯(cuò)誤處理邏輯。
集成性:WebClient 可以輕松地與 Spring Boot 的其他組件集成,如 Spring Data、Spring Security 等。這使得在構(gòu)建基于微服務(wù)的響應(yīng)式應(yīng)用程序時(shí)更加方便和靈活。
替代 RestTemplate:雖然 RestTemplate 在以前的 Spring 版本中廣泛使用,但 WebClient 被視為其現(xiàn)代替代品。WebClient 提供了更強(qiáng)大和靈活的功能,并且更適合與響應(yīng)式編程模型一起使用
一、引包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies>
二、通用方法
package com.zc.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; import java.util.List; /** * @author zc * @date 2024/4/15 16:52 * @desc */ @Component public class WebClientUtil { private final WebClient webClient; @Autowired public WebClientUtil(WebClient.Builder webClientBuilder) { this.webClient = webClientBuilder. baseUrl("http://127.0.0.1:30003"). build(); } /** * get方法 * @param url * @param responseType * @return * @param */ public <T> T get(String url, Class<T> responseType) { return webClient.get(). uri(url). accept(MediaType.APPLICATION_JSON). retrieve(). bodyToMono(responseType). block(); } /** * get多條數(shù)據(jù) * @param url * @param responseType * @return * @param <T> */ public <T> List<T> list(String url, Class<T> responseType){ return webClient.get(). uri(url). accept(MediaType.APPLICATION_JSON). retrieve(). bodyToFlux(responseType).collectList().block(); } /** * post方法 * @param url * @param requestBody * @param responseType * @return * @param */ public <T> T post(String url, Object requestBody, Class<T> responseType) { return webClient.post(). uri(url). contentType(MediaType.APPLICATION_JSON). bodyValue(requestBody). accept(MediaType.APPLICATION_JSON). retrieve(). bodyToMono(responseType). block(); } /** * put方法 * @param url * @param requestBody * @param responseType * @return * @param */ public <T> T put(String url, Object requestBody, Class<T> responseType){ return webClient.put(). uri(url). contentType(MediaType.APPLICATION_JSON). bodyValue(requestBody). accept(MediaType.APPLICATION_JSON). retrieve(). bodyToMono(responseType). block(); } /** * 刪除方法 * @param url * @param responseType * @return * @param */ public <T> T delete(String url, Class<T> responseType){ return webClient.delete(). uri(url). accept(MediaType.APPLICATION_JSON). retrieve(). bodyToMono(responseType). block(); } }
三、測(cè)試
客戶端:
import com.alibaba.fastjson.JSON; import com.zc.Application; import com.zc.bean.HostDiffBean; import com.zc.util.WebClientUtil; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.Date; import java.util.List; /** * @author zc * @date 2024/2/23 10:40 * @desc */ @SpringBootTest(classes = Application.class) public class TestFF { @Autowired private WebClientUtil webClientUtil; @Test public void test(){ List<HostDiffBean> list= webClientUtil.list("compare/hostInfo?pageSize=10&pageNum=1", HostDiffBean.class); System.out.println(JSON.toJSON(list)); HostDiffBean hostDiffBean = new HostDiffBean(); hostDiffBean.setIp("127.0.0.1"); hostDiffBean.setHIp("127.0.0.2"); hostDiffBean.setCreateTime(new Date()); hostDiffBean.setSerialNumber("123"); hostDiffBean.setHDeviceNo("no123"); hostDiffBean.setDeviceNo("NO456"); String result = webClientUtil.post("compare/hostInfo/add", hostDiffBean, String.class); System.out.println(result); } }
服務(wù)端:
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.neusoft.bean.*; import com.neusoft.service.HostDataCompareService; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; /** * @author zc * @date 2024/3/14 15:00 * @desc */ @RestController @RequestMapping("/compare") @Api(value = "數(shù)據(jù)對(duì)比接口", tags = "數(shù)據(jù)對(duì)比接口") public class DataCompareController { @Autowired private HostDataCompareService hostDataCompareService; @GetMapping("/hostInfo") @ApiOperation(value = "宿主機(jī)數(shù)量差異查詢", notes = "宿主機(jī)數(shù)量差異查詢") public List<HostInfoBean> getHostInfoBeans(@RequestParam(name = "pageSize") @Validated @ApiParam(value = "每頁(yè)數(shù)", required = true) Integer pageSize, @RequestParam(name = "pageNum") @Validated @ApiParam(value = "頁(yè)數(shù)", required = true) Integer pageNum) { Page<HostInfoBean> list = hostDataCompareService.getHostInfoBeans(pageSize, pageNum); return list.getRecords(); } @PostMapping("/hostInfo/add") @ApiOperation(value = "宿主機(jī)數(shù)量差異查詢", notes = "宿主機(jī)數(shù)量差異查詢") public String addHostInfoBeans(@RequestBody HostDiffBean hostDiffBean){ return "success"; } }
結(jié)果:
四、參考
SpringBoot - 網(wǎng)絡(luò)請(qǐng)求客戶端WebClient使用詳解
以上就是SpringBoot3實(shí)現(xiàn)webclient的通用方法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot3 webclient的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IDEA如何切換數(shù)據(jù)庫(kù)版本mysql5或mysql8
本文介紹了如何將IntelliJ IDEA從MySQL5切換到MySQL8的詳細(xì)步驟,包括下載MySQL8、安裝、配置、停止舊服務(wù)、啟動(dòng)新服務(wù)以及更改密碼等2025-01-01Java實(shí)現(xiàn)Excel導(dǎo)出并添加水印
這篇文章主要為大家詳細(xì)介紹了Java如何使用poi-ooxml包導(dǎo)出xlsx添加水印,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11Spring-boot集成pg、mongo多數(shù)據(jù)源過程詳解
這篇文章主要介紹了Spring-boot集成pg、mongo多數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Struts2中validate數(shù)據(jù)校驗(yàn)的兩種方法詳解附Struts2常用校驗(yàn)器
這篇文章主要介紹了Struts2中validate數(shù)據(jù)校驗(yàn)的兩種方法及Struts2常用校驗(yàn)器,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09