All articles
https://www.cnblogs.com/lay2017/p/11740855.html
text
Previous article, we constructed Bean instance of an object of a RestTemplate. This article will look at the core logic RestTemplate execution of the request.
To write a simple request to get sample code
@Autowired private RestTemplate restTemplate; public String getData() { return restTemplate.getForEntity("http://www.baidu.com", String.class).getBody(); }
Sample code request by getForEntity restTemplate method and returns a ResponseEntity, getBody method ResponseEntity acquisition response from the body.
getForEntity is RestOperations interface definition, RestTemplate made corresponding implementation. Let's open RestOperations look at the definition of the interface
/ ** * GET request * @param URL address request url * @param responseType response object type * @param uriVariables URI variable * @return response body * * / <T> ResponseEntity <T> getForEntity (String url, Class < T> responseType, uriVariables Object ...) throws RestClientException;
The interface simply defines what access to resources, in response to what type of data
RestTemplate follow up the implementation class, take a look at the concrete realization getForEntity
@Override public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException { // 请求回调 RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); // 响应体抽取 ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); // 核心执行逻辑 return nonNull(execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables)); }
We can see, execute contains the core logic of execution. The follow-up method
public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor, Object... uriVariables) throws RestClientException { // uri处理 URI expanded = getUriTemplateHandler().expand(url, uriVariables); // 核心逻辑 return doExecute(expanded, method, requestCallback, responseExtractor); }
Mr. execute method became a URI, then entrusted to the doExecute to deal
Follow doExecute method
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor) throws RestClientException { ClientHttpResponse response = null; try { // 生成请求 ClientHttpRequest request = createRequest(url, method); if (requestCallback != null) { // 设置header requestCallback.doWithRequest(request); } // 执行请求,获取响应 response =request.execute (); // process the response the handleResponse (URL, Method, Response); // get the response volume object return (responseExtractor =! null : responseExtractor.extractData (Response)? null ;) } the catch (IOException EX) { / / ... thrown } the finally { IF (response =! null ) { // Close response stream response.close (); } } }
Logic code doExecute process request response shows the core logic. It contains three main elements:
1) createRequest create a request object
2) request.execute () to perform the request, the fetch response data to interact with the server
3) handleResponse () method of treatment response, responseExtractor objects generated in response ResponseEntity
to sum up
We see, restTemplate good core logic follows the request-response model. DoExecute method comprising the core logic code.
Follow-up, we will open a few core of these methods take a look at the implementation details.