Why:HttpEntity is a helper object that encapsulates both the header and the body of an HTTP request or response.
2. Which methods does HttpEntity provide to access its header and body?
Why:HttpEntity provides getHeaders() to retrieve the headers and getBody() to retrieve the body.
3.HttpEntity is an alternative to using which two parameters?
Why:HttpEntity can replace the combination of @RequestHeader and @RequestBody by giving access to both headers and body in one object.
4. Can HttpEntity be used as a handler method parameter?
Why:HttpEntity can be declared as a handler method parameter, and Spring will populate it with the request headers and body.
5. What is the main advantage of using HttpEntity as a return value compared to @ResponseBody?
Why: When returning HttpEntity, you can set response headers in addition to the body, which is not possible with @ResponseBody alone.
6. Given the method signature below, what type does getBody() return when handling a String body?
public HttpEntity<String> handleRequest(HttpEntity<String> requestEntity) { ... }
Why: The generic type of HttpEntity is declared as String on the method parameter, so getBody() returns a String.
7. In the following code, what is the type of the body returned by getBody()?
public HttpEntity<User> handleUserRequest(HttpEntity<User> requestEntity) {
User user = requestEntity.getBody();
...
}
Why: The generic type of HttpEntity is User, so getBody() returns a User object.
8. Can HttpEntity be used to return a response with both a body and custom headers?
Why: You can construct an HttpEntity by passing a body and an HttpHeaders object, allowing both to be sent in the response.
9. Which class does getHeaders() return?
Why:getHeaders() returns an instance of HttpHeaders, which provides access to the request or response headers.
10. Is HttpEntity specific to Spring MVC?
Why:HttpEntity is part of the Spring Framework (org.springframework.http) and is used across Spring MVC as well as other Spring HTTP modules, rather than being exclusive to Spring MVC.
11. When HttpEntity is used as a handler method parameter, how are the request headers and body bound to it?
Why: Spring automatically resolves HttpEntity parameters by reading the request headers and body, so no additional annotations are required.
12. What is the difference between HttpEntity and @RequestBody?
Why:HttpEntity encapsulates headers and body together, whereas @RequestBody only binds the request body to a method parameter.
13. Can HttpEntity be used for both reading a request and writing a response?
Why:HttpEntity is versatile: it can represent an incoming request (as a parameter) or an outgoing response (as a return value).
14. If you want to set a custom header in a response using HttpEntity, what should you do?
Why: You can create an HttpHeaders instance, add your custom header, and then construct an HttpEntity with that headers object and the body.