发送http请求(2):RestTemplate发送http请求

简介: 发送http请求(2):RestTemplate发送http请求

image.png


关联阅读:


发送http请求(1):发送http请求的几种方式


相关组件


RestTemplate 是Spring提供的用于访问Rest服务的客户端工具,它提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。 RestTemplate 相比于他们应该算是一个整合框架,一个可以统一各种请求发送方式的框架。

先分析下其相关组件

继承体系

RestTemplate  继承了InterceptingHttpAccessor 实现了RestOperations

public class RestTemplate extends InterceptingHttpAccessor implements RestOperations {
}
RestOperations

定义rest的各种操作, 使用了大量的重载方法。例如,我们常用的

getForObject
postForObject
HttpAccessor

HttpAccessor 提供了ClientHttpRequestFactory属性,用于创建ClientHttpRequest。默认初始化SimpleClientHttpRequestFactory工厂类,

public abstract class HttpAccessor {
private ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
...
}

发送请求的几种方式上节中我们说过SimpleClientHttpRequestFactory 是针对JDK原生HttpURLConnection工厂类。也就是说RestTemplate  默认是使用HttpURLConnection来发送http请求。

不过我们可以通过set方法设置其他工厂类。换用其他发送http请求的方式。

public void setRequestFactory(ClientHttpRequestFactory requestFactory) {
        super.setRequestFactory(requestFactory);
        this.interceptingRequestFactory = null;
}
InterceptingHttpAccessor

InterceptingHttpAccessor 继承了HttpAccessor。 从其名称,我们也可以看出这是一个具有拦截器功能的HttpAccessor

public abstract class InterceptingHttpAccessor extends HttpAccessor {
  private List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
  public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
    this.interceptors = interceptors;
  }
  public List<ClientHttpRequestInterceptor> getInterceptors() {
    return interceptors;
  }
  @Override
  public ClientHttpRequestFactory getRequestFactory() {
    ClientHttpRequestFactory delegate = super.getRequestFactory();
    if (!CollectionUtils.isEmpty(getInterceptors())) {
      return new InterceptingClientHttpRequestFactory(delegate, getInterceptors());
    }
    else {
      return delegate;
    }
  }
}

getRequestFactory()方法,通过判断是否具有拦截器,决定是创建具有拦截功能的InterceptingClientHttpRequestFactory ,还是创建默认的SimpleClientHttpRequestFactory

InterceptingClientHttpRequestFactory 创建的ClientHttpRequest 是InterceptingClientHttpRequest

小结:

  • HttpAccessor: 提供对普通Http客户端 ClientHttpRequest的支持
  • InterceptingHttpAccessor: 提供对具有拦截功能的ClientHttpRequest的支持


属性体系

RestTemplate 属性也有几个关键组件,我们应该了解。

RequestCallback

Callback interface for code that operates on a ClientHttpRequest. Allows to manipulate the request headers, and write to the request body. 用于操作请求头和body,在请求发出前执行。

ResponseExtractor

解析HTTP响应的数据,从Response中提取数据,通过它来从ClientHttpResponse提取出指定内容(比如请求头、请求Body体等)

UriTemplateHandler

用来处理:Path Param 与 Query Param

ResponseErrorHandler

错误响应处理器


关联组件

InterceptingRequestExecution

当我们给Resttenplate设置拦截器时,默认会创建一个InterceptingClientHttpRequest客户端。 当我们调用InterceptingClientHttpRequest.execute()发送请求时,最终会调用InterceptingClientHttpRequest.executeInternal()方法

@Override
  protected final ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
  //根据拦截器创建一个拦截器链。
    InterceptingRequestExecution requestExecution = new InterceptingRequestExecution();
    return requestExecution.execute(this, bufferedOutput);
  }

此时创建一个拦截器执行链,并把当前(this=InterceptingClientHttpRequest) 作为参数传入到执行链中。

ClientHttpRequestInterceptor

我们看看ClientHttpRequestInterceptor 是如何执行的。 InterceptingRequestExecution#execute() 会首先执行拦截器。

InterceptingRequestExecution类
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body)  {
  if (this.iterator.hasNext()) {
    ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
    return nextInterceptor.intercept(request, body, this);
  }   
  ....  
}

拦截器执行链中,获取一个拦截器,执行其intercept方法。在每个intercept方法中都调用execution.execute() 从而形成链式调用。


请求过程


讲完了组件,下面讲讲执行过程 以getForObject为例

@Override
  public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    HttpMessageConverterExtractor<T> responseExtractor =
        new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables);
  }
  public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
      ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException {
    URI expanded = getUriTemplateHandler().expand(url, urlVariables);
    return doExecute(expanded, method, requestCallback, responseExtractor);
  }

调用doExecute方法

protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
      ResponseExtractor<T> responseExtractor) throws RestClientException {
    ClientHttpResponse response = null;
    try {
    //创建一个http请求客户端,根据
      ClientHttpRequest request = createRequest(url, method);
      if (requestCallback != null) {
      //如果requestCallback不为null。则处理request
        requestCallback.doWithRequest(request);
      }
      //执行请求
      response = request.execute();
      //处理请求,包含错误响应的处理
      handleResponse(url, method, response);
      //提前指定数据
      if (responseExtractor != null) {
        return responseExtractor.extractData(response);
      }
      else {
        return null;
      }
    }
    ... 
    finally {
      if (response != null) {
        response.close();
      }
    }
  }
  1. 首先获取到RequestCallback ,ResponseExtractor 工具。并调用UriTemplateHandler处理url中的参数问题
  2. 从HttpAccessor获取一个ClientHttpRequest,如果配置了ClientHttpRequestInterceptor,则通过InterceptingClientHttpRequestFactory创建具有拦截器功能的InterceptingClientHttpRequest客户端。如果没有使用普通的ClientHttpRequestFactory创建一个普通客户端,默认使用的是SimpleClientHttpRequestFactory 创建HttpURLConnection 的客户端
  3. 得到ClientHttpRequest后,如果RequestCallback不为null。则执行RequestCallback
  4. 执行ClientHttpRequest.execute(). 如果此时有ClientHttpRequestInterceptor,则先执行ClientHttpRequestInterceptor,再发送请求。
  5. ResponseErrorHandler 处理可能的错误响应
  6. 如果存在responseExtractor响应提取器, 则调用responseExtractor 提取response数据。
  7. 数据返回


总结


Resttemplate 应该说是对发送请求的方式的一种抽象,他不生产请求,只做请求的整合工。这种高级整合带来的是更加便捷的,更加丰富的发送请求方式。


相关文章
|
14天前
|
Java
java原生发送http请求
java原生发送http请求
|
21天前
|
网络协议 Linux iOS开发
推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
推荐:实现RTSP/RTMP/HLS/HTTP协议的轻量级流媒体框架,支持大并发连接请求
45 1
|
1月前
|
编解码 测试技术 索引
性能工具之 Jmeter 使用 HTTP 请求编写 HLS 脚本
在我们简要介绍了 HLS 协议的基础知识,接下来我们详细介绍一种使用 Jmeter 编写压测 HLS 协议脚本的方法。
72 1
性能工具之 Jmeter 使用 HTTP 请求编写 HLS 脚本
|
1月前
|
JSON 数据格式
第三方系统或者工具通过 HTTP 请求发送给 ABAP 系统的数据,应该如何解析试读版
第三方系统或者工具通过 HTTP 请求发送给 ABAP 系统的数据,应该如何解析试读版
27 0
|
1月前
|
Java Spring
用spring发送http请求
用spring发送http请求
|
3天前
|
安全 Java 网络安全
Servlet 教程 之 Servlet 客户端 HTTP 请求 2
Servlet教程介绍了如何在Servlet中处理HTTP请求,包括获取Cookie、头信息、参数、Session等。方法如:`getCookies()`、`getAttributeNames()`、`getHeaderNames()`、`getParameterNames()`等。还能获取身份验证类型、字符编码、MIME类型、请求方法、远程用户信息、URL路径、安全通道状态以及请求内容长度等。此外,可通过`getSession()`创建或获取Session,并以`Map`形式获取参数。
19 8
|
6天前
|
安全 网络安全 开发工具
对象存储oss使用问题之flutter使用http库进行post请求文件上传返回400如何解决
《对象存储OSS操作报错合集》精选了用户在使用阿里云对象存储服务(OSS)过程中出现的各种常见及疑难报错情况,包括但不限于权限问题、上传下载异常、Bucket配置错误、网络连接问题、跨域资源共享(CORS)设定错误、数据一致性问题以及API调用失败等场景。为用户降低故障排查时间,确保OSS服务的稳定运行与高效利用。
19 1
|
1月前
|
JSON 前端开发 数据格式
糊涂工具类真是场景下请求http接口的案例
糊涂工具类真是场景下请求http接口的案例
21 0
|
1月前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
20 0
|
1月前
|
网络协议 网络安全 API
Qt 网络编程之美:探索 URL、HTTP、服务发现与请求响应
Qt 网络编程之美:探索 URL、HTTP、服务发现与请求响应
45 1