background

Earlier in RestTemplate load balancing, I said I would write an article analyzing the source code of RestTemplate. This article according to the current analysis of their own source code to write.

The class diagram

Below is the RestTemplate class diagram, can be seen from the figure RestTemplate inherited InterceptingHttpAccessor, RestOperations interface is realized. The RestOperations interface includes external interfaces implemented by the RestTemplate.

InterceptingHttpAccessor inherits HttpAccessor. This class is abstract. It has no abstract methods, but a protected method:

protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
  ClientHttpRequest request = getRequestFactory().createRequest(url, method);
  if (logger.isDebugEnabled()) {
     logger.debug("HTTP " + method.name() + "" + url);
  }
  return request;
}
Copy the code

GetRequestFactory ().createrequest (URL, method) creates a ClientHttpRequest.

ClientHttpRequest is an interface that inherits the HttpRequest and HttpOutputMessage interfaces. The most important thing is that you must implement the execute() method. This method is the core method of the entire RestTemplate. ClientHttpRequest is an interface, the default implementation class there are a lot like OkHttp3ClientHttpRequest HttpComponentsClientHttpRequest, etc. Are their common implementation, if the default implementation can’t meet the demand, Can also be reference OkHttp3ClientHttpRequests on-demand implementation.

The core to realize

The execute() method of ClientHttpRequest is the core of the ClientHttpRequest.

RestTemplate calls the createRequest method of HttpAccessor to create a ClientHttpRequest. The ClientHttpRequest’s final creator is the ClientRequestFactory factory, This factory can be configured by the caller. ClientHttpRequest invokes the execute method of different subclasses. This section uses OKHttp+loadBalanced as an example. ClientHttpRequest call executeInternal method, and then create InterceptingRequestExecution object. InterceptingRequestExecution object call ClientHttpRequestInterceptort intercept method. ClientHttpRequestInterceptor is an abstract interface, the system initialization time to create. Real HTTP request is in ClientHttpRequestInterceptor implementation class. ClientHttpRequestInterceptor alone after writing an article. After the Execute method of clientHttpRequest is called, the restTemplate finally calls handleResponse to handle the return value, and the restTemplate provides the default ResponseErrorHandler to handle the response value. Developers can also create their own ResponseErrorHandler and set it to the RestTemplate during initialization.

summary

Can see RestTemplate although simple, but involves the interaction between class and class is more complex, with the help of a lot of classes and interfaces between inheritance, also can go to the factory pattern is widely used, is worth learning reference items or more, this is my first time source code analysis, write not good place still hope everybody comment.