Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between exchange method and execute method in spring rest template?

Tags:

rest

spring

I have three questions!

First.

I am using the spring framework for sending the data through rest protocol.

restTemplate.exchange(requestUrl,HttpMethod.POST, request, listVo.getClass());

org.springframework.web.client.RestTemplate.exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<? extends Object> responseType, Object... uriVariables) throws RestClientException

I used it without any problem, but I want to know the purpose of the parameter, responseType. The client don't use response data, but just use response status code / msg. So, I sent some meaningless String data instead. But the error thrown that they accept "null". So I sent a "null" String. not null. Then, the error got rid of. But there was another problem. Right after the client received the data from the server and paused for a long time. Then next line of codes are executed. What is problem?

Second

I can't find any references that use execute method of Spring RestTemplate.

Third

Like the title, What is the difference between the exchange method and the execute method in spring rest template?

Thanks for your time and effort. Cheers.

like image 753
verystrongjoe Avatar asked Dec 09 '14 12:12

verystrongjoe


People also ask

What is exchange method in RestTemplate?

The exchange method executes the request of any HTTP method and returns ResponseEntity instance. The exchange method can be used for HTTP DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE methods. Using exchange method we can perform CRUD operation i.e. create, read, update and delete data.

What is difference between getForObject and getForEntity?

For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.

What is the difference between postForObject and postForEntity?

Compared to postForObject(), postForEntity() returns the response as a ResponseEntity object. Other than that, both methods do the same job. Let's say that we want to make a POST request to our Person API to create a new Person object and return the response as a ResponseEntity.


1 Answers

exchange return type is ResponseEntity<T> while execute is T

like image 54
Bobo Avatar answered Sep 23 '22 06:09

Bobo