What's the difference between ResponseEntity
and HttpEntity
in Spring? I want to know the difference between the two and when to use them in a Spring MVC controller.
HttpEntity can be used to create both RequestEntity and ResponseEntity.
Where as ResponseEntity is subclassed from HttpEntity with a more elaborative way to send the ResponseObject and it only limited to sending the Response. Some Key differences are below :
ResponseEntity inherited from HttpEntity that has an additional HttpStatus Code while Sending the ResponseEntity Object.
Also it has ResponseEntity.BodyBuilder which adds body to the response Object and ResponseEntity.HeadersBuilder which adds header to the Response object.
An HTTP entity is the majority of an HTTP request or response, consisting of some of the headers and the body, if present. It seems to be the entire request or response without the request or status line (although only certain header fields are considered part of the entity).
while ResponseEntity is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body.
HttpEntity wraps both the request and response message. But the ResponseEntity wraps only the response message. ResponseEntity basically inherits from HttpEntity. Response Entity can also have a HTTP status code unlike its parent class. ex:
new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With