What is the restTemplate.exchange() method for?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)