I am currently sending a resource to a client, I am using code that has been done already and I am modifying it, there is a line shown below in this code that I don't understand. Well I understand that I am sending or posting a resource, I understand this method takes the url of the client, that it takes the type of HTTP request for example in this case POST, but I dont understant why this method takes nService.getStringHttpEntityWithPayload(payLoad) and Resource.class? Also the response entity it is returning will it be a class only or a class with a status and a headers?
ResponseEntity<Resource> responseEntity = restTemplate.exchange(
eURL,
HttpMethod.POST,
nService.getStringHttpEntityWithPayload(payLoad),
Resource.class);
why this method takes nService.getStringHttpEntityWithPayload(payLoad) and Resource.class?
The method getStringHttpEntityWithPayload
is returning a HttpEntity
which is composed of a body and header data to be sent to a URL. The method is creating the request message by adding the content type header, letting the receiving service know that the body contains JSON data.
The parameter Resource.class
is used to determine what class to deserialize the response body from the service into. It defines the generic type of the return value: ResponseEntity<Resource>
.
Also the response entity it is returning will it be a class only or a class with a status and a headers?
I'm not sure what you mean by "class only". The ResponseEntity
is similar to HttpEntity
(in fact class ResponseEntity<T> extends HttpEntity<T>
). The ResponseEntity
class contains the response body and headers, as well as the HTTP Status code of the response.