最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot3實(shí)現(xiàn)webclient的通用方法詳解

 更新時(shí)間:2024年04月17日 09:52:22   作者:面壁者-揚(yáng)  
Spring Boot WebClient 是 Spring Framework 5 中引入的一個(gè)新的響應(yīng)式 Web 客戶端,用于異步和響應(yīng)式地與外部服務(wù)進(jìn)行通信,下面我們就來(lái)看看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 等),并提供了豐富的功能來(lái)處理請(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)文章

  • pagehelper踩坑記之分頁(yè)亂套問(wèn)題解決

    pagehelper踩坑記之分頁(yè)亂套問(wèn)題解決

    這篇文章主要為大家介紹了pagehelper踩坑記之分頁(yè)亂套問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • @JsonFormat?和?@DateTimeFormat?時(shí)間格式化注解(場(chǎng)景示例代碼)

    @JsonFormat?和?@DateTimeFormat?時(shí)間格式化注解(場(chǎng)景示例代碼)

    這篇文章主要介紹了@JsonFormat和@DateTimeFormat時(shí)間格式化注解,本文通過(guò)場(chǎng)景示例代碼詳解給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • java final本質(zhì)詳解

    java final本質(zhì)詳解

    在本篇文章里小編給大家分享的是關(guān)于java final本質(zhì)的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。
    2019-09-09
  • 從零開(kāi)始:快速入門SpringBoot注解的精髓

    從零開(kāi)始:快速入門SpringBoot注解的精髓

    Spring?Boot是一個(gè)用于快速構(gòu)建基于Spring框架的應(yīng)用程序的開(kāi)源框架,它通過(guò)使用注解來(lái)簡(jiǎn)化配置和開(kāi)發(fā)過(guò)程,使開(kāi)發(fā)人員能夠更加專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),Spring?Boot提供了許多注解,用于定義和配置應(yīng)用程序的各個(gè)方面,需要的朋友可以參考下
    2023-10-10
  • Spring中Bean的生命周期使用解析

    Spring中Bean的生命周期使用解析

    這篇文章主要介紹了Spring中Bean的生命周期使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java編程實(shí)現(xiàn)時(shí)間和時(shí)間戳相互轉(zhuǎn)換實(shí)例

    Java編程實(shí)現(xiàn)時(shí)間和時(shí)間戳相互轉(zhuǎn)換實(shí)例

    這篇文章主要介紹了什么是時(shí)間戳,以及Java編程實(shí)現(xiàn)時(shí)間和時(shí)間戳相互轉(zhuǎn)換實(shí)例,具有一定的參考價(jià)值,需要的朋友可以了解下。
    2017-09-09
  • SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例

    SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例

    這篇文章主要介紹了SSM項(xiàng)目中使用攔截器和過(guò)濾器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Java數(shù)組高級(jí)算法與Arrays類常見(jiàn)操作小結(jié)【排序、查找】

    Java數(shù)組高級(jí)算法與Arrays類常見(jiàn)操作小結(jié)【排序、查找】

    這篇文章主要介紹了Java數(shù)組高級(jí)算法與Arrays類常見(jiàn)操作,結(jié)合實(shí)例形式總結(jié)分析了Java數(shù)組常見(jiàn)的排序算法、查找算法相關(guān)原理、實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2019-03-03
  • JavaWeb之Filter過(guò)濾器詳解

    JavaWeb之Filter過(guò)濾器詳解

    本篇文章主要介紹了JavaWeb之Filter過(guò)濾器詳解,實(shí)例分析了JavaWeb之Filter過(guò)濾器的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • Java使用Poi導(dǎo)出Excel表格方法實(shí)例

    Java使用Poi導(dǎo)出Excel表格方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Java使用Poi導(dǎo)出Excel表格的相關(guān)資料,Java POI是一個(gè)用于操作Microsoft Office格式的Java API庫(kù),可以使用它來(lái)導(dǎo)出Excel文件,需要的朋友可以參考下
    2023-10-10

最新評(píng)論

岳西县| 昭苏县| 望谟县| 苗栗县| 聂荣县| 房产| 铁岭县| 日喀则市| 武夷山市| 彰武县| 洪洞县| 彰武县| 崇信县| 紫金县| 彩票| 刚察县| 石首市| 花莲县| 巧家县| 临桂县| 称多县| 襄汾县| 平顶山市| 大同市| 祁门县| 长岛县| 红桥区| 涿州市| 安阳市| 大理市| 鄂温| 柘荣县| 宁阳县| 云安县| 柳州市| 平利县| 旬邑县| 揭东县| 全椒县| 西乌| 兰州市|