RestTemplate-postForObject源码
参数: 请求路径, 请求参数, 返回类型, 扩展模板变量
参数: 请求参数, 返回类型
HttpEntityRequestCallback构造器
HttpEntityRequestCallback是RestTemplate的私有内部类,继承了RestTemplate私有内部类 AcceptHeaderRequestCallback,AcceptHeaderRequestCallback实现了RequestCallback接口,重写了doWithRequest方法
HttpEntity是 HTTP请求或响应实体
public void doWithRequest(ClientHttpRequest httpRequest) throws IOException {
//调用父类方法
super.doWithRequest(httpRequest);
Object requestBody = this.requestEntity.getBody();
//判断内部类中的HttpEntity是否有参数
if (requestBody == null) {
HttpHeaders httpHeaders = httpRequest.getHeaders();
HttpHeaders requestHeaders = this.requestEntity.getHeaders();
if (!requestHeaders.isEmpty()) {
//如果header不为空,将header的值赋给方法接收的参数ClientHttpRequest
requestHeaders.forEach((key, values) -> {
httpHeaders.put(key, new LinkedList(values));
});
}
//添加content长度
if (httpHeaders.getContentLength() < 0L) {
httpHeaders.setContentLength(0L);
}
} else {
Class<?> requestBodyClass = requestBody.getClass();
Type requestBodyType = this.requestEntity instanceof RequestEntity ? ((RequestEntity)this.requestEntity).getType() : requestBodyClass;
HttpHeaders httpHeadersx = httpRequest.getHeaders();
HttpHeaders requestHeadersx = this.requestEntity.getHeaders();
MediaType requestContentType = requestHeadersx.getContentType();
//消息转换器列表
Iterator var8 = RestTemplate.this.getMessageConverters().iterator();
while(var8.hasNext()) {
//策略接口,指定可以从HTTP请求和响应转换的转换器
HttpMessageConverter<?> messageConverter = (HttpMessageConverter)var8.next();
if (messageConverter instanceof GenericHttpMessageConverter) {
//可以将HTTP请求转换为指定泛型类型的目标对象,将指定泛型类型的源对象转换为HTTP响应
GenericHttpMessageConverter<Object> genericConverter = (GenericHttpMessageConverter)messageConverter;
//判断 指示此转换器是否可以写入给定的类
if (genericConverter.canWrite((Type)requestBodyType, requestBodyClass, requestContentType)) {
if (!requestHeadersx.isEmpty()) {
requestHeadersx.forEach((key, values) -> {
httpHeadersx.put(key, new LinkedList(values));
});
}
//打印debug日志
this.logBody(requestBody, requestContentType, genericConverter);
将给定对象写入给定的输出消息
genericConverter.write(requestBody, (Type)requestBodyType, requestContentType, httpRequest);
return;
}
} else if (messageConverter.canWrite(requestBodyClass, requestContentType)) {
if (!requestHeadersx.isEmpty()) {
requestHeadersx.forEach((key, values) -> {
httpHeadersx.put(key, new LinkedList(values));
});
}
this.logBody(requestBody, requestContentType, messageConverter);
messageConverter.write(requestBody, requestContentType, httpRequest);
return;
}
}
String message = "No HttpMessageConverter for [" + requestBodyClass.getName() + "]";
if (requestContentType != null) {
message = message + " and content type [" + requestContentType + "]";
}
throw new RestClientException(message);
}
}
private void logBody(Object body, @Nullable MediaType mediaType, HttpMessageConverter<?> converter) {
if (RestTemplate.this.logger.isDebugEnabled()) {
if (mediaType != null) {
RestTemplate.this.logger.debug("Writing [" + body + "] as \"" + mediaType + "\"");
} else {
String classname = converter.getClass().getName();
RestTemplate.this.logger.debug("Writing [" + body + "] with " + classname);
}
}
}
响应提取器,它使用给定的实体转换器 将响应转换为类型T
最后的execute方法
@Nullable
public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException {
//使用URI变量映射展开给定的URI模板
URI expanded = this.getUriTemplateHandler().expand(url, uriVariables);
return this.doExecute(expanded, method, requestCallback, responseExtractor);
}
参数:url - 要连接的完全扩展的URL, method - 要执行的HTTP方法(GET,POST等), requestCallback- 准备请求的对象(可以null), responseExtractor- 从响应中提取返回值的对象(可以null)
@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "URI is required");
Assert.notNull(method, "HttpMethod is required");
ClientHttpResponse response = null;
Object var14;
try {
//通过工厂模式创建ClientHttpRequest对象
ClientHttpRequest request = this.createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
//执行请求
response = request.execute();
//处理给定的响应,执行适当的日志记录并在ResponseErrorHandler必要时调用
this.handleResponse(url, method, response);
//extractData 从给定数据中提取数据ClientHttpResponse并将其返回
var14 = responseExtractor != null ? responseExtractor.extractData(response) : null;
} catch (IOException var12) {
String 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 var14;
}
博客链接:https://www.cnblogs.com/gqymy/