Class HttpEntity<T>
java.lang.Object
org.springframework.http.HttpEntity<T>
- Type Parameters:
- T- the body type
- Direct Known Subclasses:
- RequestEntity, ResponseEntity
Represents an HTTP request or response entity, consisting of headers and body.
Often used in combination with the RestTemplate,
like so:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> entity = new HttpEntity<>("Hello World", headers);
URI location = template.postForLocation("https://example.com", entity);
or
HttpEntity<String> entity = template.getForEntity("https://example.com", String.class);
String body = entity.getBody();
MediaType contentType = entity.getHeaders().getContentType();
Can also be used in Spring MVC, as a return value from a @Controller method:
@GetMapping("/handle")
public HttpEntity<String> handle() {
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.set("MyResponseHeader", "MyValue");
  return new HttpEntity<>("Hello World", responseHeaders);
}
- Since:
- 3.0.2
- Author:
- Arjen Poutsma, Juergen Hoeller
- See Also:
- 
Field SummaryFields
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedCreate a new, emptyHttpEntity.HttpEntity(@Nullable T body, @Nullable HttpHeaders headers) Create a newHttpEntitywith the given body and headers.HttpEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers) Deprecated, for removal: This API element is subject to removal in a future version.HttpEntity(HttpHeaders headers) Create a newHttpEntitywith the given headers and no body.HttpEntity(MultiValueMap<String, String> headers) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofHttpEntity(HttpHeaders)HttpEntity(T body) Create a newHttpEntitywith the given body and no headers.
- 
Method Summary
- 
Field Details- 
EMPTY
 
- 
- 
Constructor Details- 
HttpEntityprotected HttpEntity()Create a new, emptyHttpEntity.
- 
HttpEntityCreate a newHttpEntitywith the given body and no headers.- Parameters:
- body- the entity body
 
- 
HttpEntityCreate a newHttpEntitywith the given headers and no body.- Parameters:
- headers- the entity headers
- Since:
- 7.0
 
- 
HttpEntityCreate a newHttpEntitywith the given body and headers.- Parameters:
- body- the entity body
- headers- the entity headers
- Since:
- 7.0
 
- 
HttpEntityDeprecated, for removal: This API element is subject to removal in a future version.in favor ofHttpEntity(HttpHeaders)Create a newHttpEntitywith the given headers and no body.- Parameters:
- headers- the entity headers
 
- 
HttpEntity@Deprecated(since="7.0", forRemoval=true) public HttpEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofHttpEntity(Object, HttpHeaders)Create a newHttpEntitywith the given body and headers.- Parameters:
- body- the entity body
- headers- the entity headers
 
 
- 
- 
Method Details- 
getHeadersReturns the headers of this entity.
- 
getBody
- 
hasBodypublic boolean hasBody()Indicates whether this entity has a body.
- 
equals
- 
hashCode
- 
toString
 
- 
HttpEntity(Object, HttpHeaders)