What is the restTemplate.exchange() method for?

前端 未结 4 1216
日久生厌
日久生厌 2021-02-02 07:37

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

@RequestMapping(value = \"/getphoto\", method = R         


        
4条回答
  •  时光说笑
    2021-02-02 08:14

    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 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.

提交回复
热议问题