Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the restTemplate.exchange() method for?

Actually what does the restTemplate.exchange() method do?

@RequestMapping(value = "/getphoto", method = RequestMethod.GET) public void getPhoto(@RequestParam("id") Long id, HttpServletResponse response) {      logger.debug("Retrieve photo with id: " + id);      // Prepare acceptable media type     List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();     acceptableMediaTypes.add(MediaType.IMAGE_JPEG);      // Prepare header     HttpHeaders headers = new HttpHeaders();     headers.setAccept(acceptableMediaTypes);     HttpEntity<String> entity = new HttpEntity<String>(headers);      // Send the request as GET     ResponseEntity<byte[]> result =          restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}",                                HttpMethod.GET, entity, byte[].class, id);      // Display the image     Writer.write(response, result.getBody()); } 
like image 648
sneha Avatar asked Nov 25 '13 07:11

sneha


People also ask

What does RestTemplate exchange method do?

Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods. The code given below shows how to create Bean for Rest Template to auto wiring the Rest Template object.

How do you use RestTemplate POST method?

Posting JSON With postForObject. RestTemplate's postForObject method creates a new resource by posting an object to the given URI template. It returns the result as automatically converted to the type specified in the responseType parameter.

Is RestTemplate exchange thread safe?

RestTemplate is thread-safe once constructed. Objects of the RestTemplate class do not change any of their state information to process HTTP: the class is an instance of the Strategy design pattern.


2 Answers

TL;DR: Q: What is a request-response pair called? A: An "exchange".


The term exchange is used, almost incidentally, in the official technical documentation of HTTP to refer to an HTTP request combined with the corresponding response.

However looking at the answers to the following questions, it is clear that while this may have represented a de facto standard for some people, many other were not aware of it, or hadn't adopted it.

  • What is a request-response pair called?
  • Name for HTTP Request+Response

The documentation doesn't bother to mention the etymology of the name -- probably assuming that it's obvious.

Notice, however, that there are many different RestTemplate HTTP request methods listed and only a small fraction of them are named exchange. The list is primarily made up of HTTP method-specific names such as delete, put, getForEntity, postForObject, et cetera. Technically speaking, all of these methods perform exchanges in the same sense, but the more focused convenience methods are limited to a specific subset of the possible exchange functionality and parameter+return types.

To put it simply, the set of exchange functions are the most general/capable methods provided by RestTemplate, so you can use exchange when none of the other methods provides a complete enough parameter set to meet your needs.

For example:

  • Sending GET request with Authentication headers using restTemplate, in which the OP has noticed that "...the only way to send Headers such as accept and Authorization is by using the exchange method..."
like image 27
Brent Bradburn Avatar answered Oct 19 '22 03:10

Brent Bradburn


The method documentation is pretty straightforward:

Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity.

URI Template variables are expanded using the given URI variables, if any.


Consider the following code extracted from your own question:

ResponseEntity<byte[]> result =      restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}",                            HttpMethod.GET, entity, byte[].class, id); 

We have the following:

  • A GET request will be performed to the given URL sending the HTTP headers that are wrapped in the HttpEntity instance.
  • The given URL contains a template variable ({id}). It will be replaced with the value given in the last method parameter (id).
  • The response entity will be returned​ as a byte[] wrapped into a ResponseEntity instance.
like image 168
cassiomolin Avatar answered Oct 19 '22 03:10

cassiomolin