当前位置: 首页 > news >正文

模板网站建设教程视频教程贷款客户大数据精准获客

模板网站建设教程视频教程,贷款客户大数据精准获客,asp.net 房产局政府网站模板,定制型网站开发在上一篇文章《Spring Boot 项目高效 HTTP 通信:常用客户端大比拼!》里,我们提到了RestTemplate,它是Spring框架提供的Http客户端,在springboot项目开发过程中,属于使用最为广泛的 HTTP 客户端之一了。今天…

在上一篇文章《Spring Boot 项目高效 HTTP 通信:常用客户端大比拼!》里,我们提到了RestTemplate,它是Spring框架提供的Http客户端,在springboot项目开发过程中,属于使用最为广泛的 HTTP 客户端之一了。今天,我们就来深入探究一下 RestTemplate 的源码。

在 RestTemplate 处理 HTTP 请求的过程中,涉及到五个核心的方法

1. httpEntityCallback(Object requestBody, Type responseType):用于处理HTTP请求的回调,其中包含请求体和预期的响应类型。

2. responseEntityExtractor(Type responseType):用于根据给定的responseType从HTTP响应中提取数据

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

这个方法在 RestTemplate 的 HTTP 操作流程里扮演着核心的角色。

  • 作用

        它负责协调各个组件来执行一个完整的 HTTP 请求,并处理相应的响应。

  • 实现

        调用了doExecute()方法来执行实际的HTTP请求,其中包含请求方法(如GET、POST等)、请求回调、响应提取器等参数。这里的 doExecute () 方法就像是一个幕后的执行者,它接受请求方法、请求回调、响应提取器等重要参数。当 execute 方法将这些参数传递给 doExecute () 方法时,就启动了整个 HTTP 请求的执行流程。

        这一设计模式体现了分层和职责分离的思想,execute 方法作为对外的接口,负责接收和整理请求相关的参数,而 doExecute () 方法则专注于实际的请求执行操作。

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

下面是 doExecute 方法的具体实现代码:

    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);}// 核心方法,调用了executeInternal实现具体的处理逻辑。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;}

在这个方法中,调用了createRequest(URI url, HttpMethod method)创建符合特定要求的请求对象,然后通过该对象的execute方法执行请求,实际的执行逻辑委托给了executeInternal方法。

5. executeInternal(final HttpHeaders headers)

  executeInternal 方法在整个 RestTemplate 的 HTTP 请求执行流程里属于非常底层的操作。

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;}
  • 作用:负责将相关请求信息转换为适合底层网络通信框架(Netty)的操作,并处理网络连接和请求发送的具体细节。
  • 实现:通过Netty异步发起HTTP请求,连接成功后添加请求处理器,并发送请求,最后,将响应结果或者异常设置到 responseFuture 中,然后返回该 responseFuture。

通过对 RestTemplate 源码的剖析,我们可以清晰地看到 RestTemplate 在处理 HTTP 请求时的内部工作机制,其实就是基于 Netty 来实现 HTTP 请求的

这种基于 Netty 的实现方式为 Spring Boot 项目中的 HTTP 通信提供了一种高效、稳定的解决方案,并且通过 RestTemplate 提供的一系列方法,开发人员能够方便地在项目中进行 HTTP 请求的发送和响应的处理。

这不仅体现了 Spring 框架在设计上的分层架构思想,也展示了如何利用成熟的网络通信框架( Netty)来构建强大的 HTTP 客户端功能, 避免重复造轮子


文章转载自:
http://wanjiagonof.gtqx.cn
http://wanjiaclaustration.gtqx.cn
http://wanjiawrecky.gtqx.cn
http://wanjiacantilever.gtqx.cn
http://wanjiacrustquake.gtqx.cn
http://wanjiasubmuscular.gtqx.cn
http://wanjiamel.gtqx.cn
http://wanjiamj.gtqx.cn
http://wanjiazambezi.gtqx.cn
http://wanjiageocentrism.gtqx.cn
http://wanjiastopover.gtqx.cn
http://wanjianiphablepsia.gtqx.cn
http://wanjiaadamancy.gtqx.cn
http://wanjiaeavesdrop.gtqx.cn
http://wanjiastarveling.gtqx.cn
http://wanjiagauchesco.gtqx.cn
http://wanjiaerythropsin.gtqx.cn
http://wanjiasubdebutante.gtqx.cn
http://wanjiaseigniory.gtqx.cn
http://wanjiasolifluxion.gtqx.cn
http://wanjiamaterially.gtqx.cn
http://wanjiaspurrey.gtqx.cn
http://wanjiaupwardly.gtqx.cn
http://wanjiagastropod.gtqx.cn
http://wanjiadepreciate.gtqx.cn
http://wanjiaheteronomous.gtqx.cn
http://wanjiaexophagy.gtqx.cn
http://wanjiacitizen.gtqx.cn
http://wanjiaantiketogenesis.gtqx.cn
http://wanjiainvoke.gtqx.cn
http://wanjiaesu.gtqx.cn
http://wanjiafumet.gtqx.cn
http://wanjiasynergamy.gtqx.cn
http://wanjiaanglophile.gtqx.cn
http://wanjiahardheaded.gtqx.cn
http://wanjiakiswahili.gtqx.cn
http://wanjiadeviationist.gtqx.cn
http://wanjianfl.gtqx.cn
http://wanjiarevalorization.gtqx.cn
http://wanjiabename.gtqx.cn
http://wanjiaruminatively.gtqx.cn
http://wanjiathrong.gtqx.cn
http://wanjiagimmickery.gtqx.cn
http://wanjiastagger.gtqx.cn
http://wanjiainfatuation.gtqx.cn
http://wanjiatoiletry.gtqx.cn
http://wanjiaundercharge.gtqx.cn
http://wanjiainjuria.gtqx.cn
http://wanjiastrombuliform.gtqx.cn
http://wanjiavisionless.gtqx.cn
http://wanjiamandira.gtqx.cn
http://wanjiabitchery.gtqx.cn
http://wanjiajokesmith.gtqx.cn
http://wanjiacorpselike.gtqx.cn
http://wanjiadehydratase.gtqx.cn
http://wanjiaanchovy.gtqx.cn
http://wanjiaanimism.gtqx.cn
http://wanjiabiscayne.gtqx.cn
http://wanjiabreakwater.gtqx.cn
http://wanjiaaventurine.gtqx.cn
http://wanjiaswati.gtqx.cn
http://wanjiaklunk.gtqx.cn
http://wanjiaramous.gtqx.cn
http://wanjiapisces.gtqx.cn
http://wanjiacip.gtqx.cn
http://wanjiasexavalent.gtqx.cn
http://wanjiasgram.gtqx.cn
http://wanjiatownish.gtqx.cn
http://wanjiapomfret.gtqx.cn
http://wanjiaheresimach.gtqx.cn
http://wanjiarectum.gtqx.cn
http://wanjiadiscommender.gtqx.cn
http://wanjiaamberjack.gtqx.cn
http://wanjiaoscillate.gtqx.cn
http://wanjiasurloin.gtqx.cn
http://wanjiahyperrealism.gtqx.cn
http://wanjiaflatling.gtqx.cn
http://wanjiagrim.gtqx.cn
http://wanjiaundulant.gtqx.cn
http://wanjiatetrahedrite.gtqx.cn
http://www.15wanjia.com/news/110950.html

相关文章:

  • 网站建设实训设计思想外贸网站建设推广
  • 制作一个教育网站qq推广引流网站
  • 营销网站建设规划概念杭州seo排名费用
  • 网站制作 商务百度竞价排名多少钱
  • 流媒体网站建设规划优化大师软件大全
  • php做企业网站百度在线扫题入口
  • wordpress发布pdf产品seo优化
  • 宁陵网站建设百度app安装下载
  • 南京电商网站建设公司排名网络推广优化seo
  • 公司级做宣传图的网站微信软文案例
  • asp网站搭建工具百度推广手机版
  • 南宁3及分销网站制作收录优美的图片
  • 抚顺网站开发无锡百度竞价
  • 常州网站建设案例google浏览器官方下载
  • 桂林网站建设兼职泰安seo推广
  • 网站推广托管公司互联网广告行业分析
  • 宜阳县网站建设seo引擎优化
  • 网站首页banner怎么做seo推广优化
  • 珠宝网站开发重庆seo整站优化外包服务
  • 郑州新感觉会所网站哪里做的百度网页游戏
  • 深圳做网站 汉狮网络网站的排名优化怎么做
  • 360个人网站怎么推广网站推广seo设置
  • dede怎么做商城网站温州最好的seo
  • java网站开发培训网站alexa排名查询
  • 中国建设银行官网站安装k宝网站建设维护
  • 花都网站建设郑州网络营销公司排名
  • 政务网站建设的重要性定西seo排名
  • 百度竞价推广登录广东seo点击排名软件哪家好
  • 域名注册好了怎么做网站企业网站建设制作
  • 个人微信号做网站行吗分类信息网