中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

常州做網(wǎng)站哪家便宜廣州百度推廣優(yōu)化排名

常州做網(wǎng)站哪家便宜,廣州百度推廣優(yōu)化排名,東莞 網(wǎng)站制作,小江高端網(wǎng)站建設(shè)在上一篇文章《Spring Boot 項目高效 HTTP 通信:常用客戶端大比拼!》里,我們提到了RestTemplate,它是Spring框架提供的Http客戶端,在springboot項目開發(fā)過程中,屬于使用最為廣泛的 HTTP 客戶端之一了。今天…

在上一篇文章《Spring Boot 項目高效 HTTP 通信:常用客戶端大比拼!》里,我們提到了RestTemplate,它是Spring框架提供的Http客戶端,在springboot項目開發(fā)過程中,屬于使用最為廣泛的 HTTP 客戶端之一了。今天,我們就來深入探究一下 RestTemplate 的源碼。

在 RestTemplate 處理 HTTP 請求的過程中,涉及到五個核心的方法。

1. httpEntityCallback(Object requestBody, Type responseType):用于處理HTTP請求的回調(diào),其中包含請求體和預期的響應(yīng)類型。

2.?responseEntityExtractor(Type responseType):用于根據(jù)給定的responseType從HTTP響應(yīng)中提取數(shù)據(jù)。

3.?execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Object... uriVariables)方法

這個方法在 RestTemplate 的 HTTP 操作流程里扮演著核心的角色。

  • 作用

????????它負責協(xié)調(diào)各個組件來執(zhí)行一個完整的 HTTP 請求,并處理相應(yīng)的響應(yīng)。

  • 實現(xiàn)

????????調(diào)用了doExecute()方法來執(zhí)行實際的HTTP請求,其中包含請求方法(如GET、POST等)、請求回調(diào)、響應(yīng)提取器等參數(shù)。這里的 doExecute () 方法就像是一個幕后的執(zhí)行者,它接受請求方法、請求回調(diào)、響應(yīng)提取器等重要參數(shù)。當 execute 方法將這些參數(shù)傳遞給 doExecute () 方法時,就啟動了整個 HTTP 請求的執(zhí)行流程。

????????這一設(shè)計模式體現(xiàn)了分層和職責分離的思想,execute 方法作為對外的接口,負責接收和整理請求相關(guān)的參數(shù),而 doExecute () 方法則專注于實際的請求執(zhí)行操作。

4. doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor)

下面是 doExecute 方法的具體實現(xiàn)代碼:

    protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {Assert.notNull(url, "'url' must not be null");Assert.notNull(method, "'method' must not be null");ClientHttpResponse response = null;String resource;try {// 核心邏輯ClientHttpRequest request = this.createRequest(url, method);if (requestCallback != null) {requestCallback.doWithRequest(request);}// 核心方法,調(diào)用了executeInternal實現(xiàn)具體的處理邏輯。response = request.execute();this.handleResponse(url, method, response);if (responseExtractor != null) {Object var14 = responseExtractor.extractData(response);return var14;}resource = null;} catch (IOException var12) {resource = url.toString();String query = url.getRawQuery();resource = query != null ? resource.substring(0, resource.indexOf(63)) : resource;throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + resource + "\": " + var12.getMessage(), var12);} finally {if (response != null) {response.close();}}return resource;}

在這個方法中,調(diào)用了createRequest(URI url, HttpMethod method)創(chuàng)建符合特定要求的請求對象,然后通過該對象的execute方法執(zhí)行請求,實際的執(zhí)行邏輯委托給了executeInternal方法。

5.?executeInternal(final HttpHeaders headers)

??executeInternal 方法在整個 RestTemplate 的 HTTP 請求執(zhí)行流程里屬于非常底層的操作。

protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {final SettableListenableFuture<ClientHttpResponse> responseFuture = new SettableListenableFuture();ChannelFutureListener connectionListener = new ChannelFutureListener() {public void operationComplete(ChannelFuture future) throws Exception {if (future.isSuccess()) {Channel channel = future.channel();channel.pipeline().addLast(new ChannelHandler[]{new RequestExecuteHandler(responseFuture)});FullHttpRequest nettyRequest = Netty4ClientHttpRequest.this.createFullHttpRequest(headers);channel.writeAndFlush(nettyRequest);} else {responseFuture.setException(future.cause());}}};// netty啟動器this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);return responseFuture;}
  • 作用:負責將相關(guān)請求信息轉(zhuǎn)換為適合底層網(wǎng)絡(luò)通信框架(Netty)的操作,并處理網(wǎng)絡(luò)連接和請求發(fā)送的具體細節(jié)。
  • 實現(xiàn):通過Netty異步發(fā)起HTTP請求,連接成功后添加請求處理器,并發(fā)送請求,最后,將響應(yīng)結(jié)果或者異常設(shè)置到 responseFuture 中,然后返回該 responseFuture。

通過對 RestTemplate 源碼的剖析,我們可以清晰地看到 RestTemplate 在處理 HTTP 請求時的內(nèi)部工作機制,其實就是基于 Netty 來實現(xiàn) HTTP 請求的。

這種基于 Netty 的實現(xiàn)方式為 Spring Boot 項目中的 HTTP 通信提供了一種高效、穩(wěn)定的解決方案,并且通過 RestTemplate 提供的一系列方法,開發(fā)人員能夠方便地在項目中進行 HTTP 請求的發(fā)送和響應(yīng)的處理。

這不僅體現(xiàn)了 Spring 框架在設(shè)計上的分層架構(gòu)思想,也展示了如何利用成熟的網(wǎng)絡(luò)通信框架(?Netty)來構(gòu)建強大的 HTTP 客戶端功能, 避免重復造輪子。

http://www.risenshineclean.com/news/59442.html

相關(guān)文章:

  • 六安做網(wǎng)站公司深圳seo論壇
  • 公司做外貿(mào)的網(wǎng)站網(wǎng)站免費制作平臺
  • 正規(guī)的錦州網(wǎng)站建設(shè)東莞百度seo推廣公司
  • 做網(wǎng)站思路sem競價培訓
  • 靜態(tài)網(wǎng)站怎么做優(yōu)化平臺優(yōu)化是指什么
  • 廣州網(wǎng)站建設(shè)星珀泰安網(wǎng)站推廣優(yōu)化
  • 深圳樂安居網(wǎng)站誰做的北京網(wǎng)站推廣營銷服務(wù)電話
  • 邢臺建設(shè)網(wǎng)站如何做百度關(guān)鍵詞推廣
  • 微網(wǎng)站促銷版cps推廣
  • 石家莊做商城網(wǎng)站的公司網(wǎng)站點擊量與排名
  • 自己做網(wǎng)站流程有道搜索引擎入口
  • 吉林市網(wǎng)站推廣百度宣傳推廣費用
  • 織夢網(wǎng)站 聯(lián)系方式修改今天的病毒感染情況
  • 湖北住房和城鄉(xiāng)建設(shè)部網(wǎng)站直接下載app
  • 網(wǎng)站備案在外地怎樣創(chuàng)建網(wǎng)站平臺
  • 兩學一做注冊網(wǎng)站嗎網(wǎng)絡(luò)廣告有哪些形式
  • 平面設(shè)計比較好的網(wǎng)站廣告投放平臺系統(tǒng)
  • 金色世紀做網(wǎng)站的是哪個崗位發(fā)布推廣信息的網(wǎng)站
  • 在縣城怎么做網(wǎng)站公司seo教程優(yōu)化
  • 做網(wǎng)站不打廣告怎么賺錢網(wǎng)絡(luò)營銷是什么工作
  • 公司微網(wǎng)站怎么建設(shè)廣州線上教學
  • 動易網(wǎng)站后臺密碼破解寧夏百度推廣代理商
  • 航達建設(shè)網(wǎng)站上海優(yōu)質(zhì)網(wǎng)站seo有哪些
  • 有沒有什么做統(tǒng)計的網(wǎng)站網(wǎng)絡(luò)營銷比較好的企業(yè)
  • 如何解決網(wǎng)站只收錄首頁的一些辦法小程序制作
  • cms網(wǎng)站地圖模板東莞搜索引擎推廣
  • 開淘寶店和做網(wǎng)站有什么區(qū)別福鼎網(wǎng)站優(yōu)化公司
  • 直接打域名訪問網(wǎng)站關(guān)鍵詞推廣工具
  • 網(wǎng)站制作設(shè)及的技術(shù)產(chǎn)品營銷策劃方案3000字
  • 廣州定制網(wǎng)站建設(shè)廣東seo網(wǎng)站推廣代運營