public class HttpEntity<T> extends Object
Typically used in combination with the RestTemplate
,
like so:
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<String>(helloWorld, 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:
@RequestMapping("/handle") public HttpEntity<String> handle() { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("MyResponseHeader", "MyValue"); return new HttpEntity<String>("Hello World", responseHeaders); }
RestTemplate
,
getBody()
,
getHeaders()
Modifier and Type | Field and Description |
---|---|
static HttpEntity<?> |
EMPTY
The empty
HttpEntity , with no body or headers. |
Modifier | Constructor and Description |
---|---|
protected |
HttpEntity()
Create a new, empty
HttpEntity . |
|
HttpEntity(MultiValueMap<String,String> headers)
Create a new
HttpEntity with the given headers and no body. |
|
HttpEntity(T body)
Create a new
HttpEntity with the given body and no headers. |
|
HttpEntity(T body,
MultiValueMap<String,String> headers)
Create a new
HttpEntity with the given body and headers. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other) |
T |
getBody()
Returns the body of this entity.
|
HttpHeaders |
getHeaders()
Returns the headers of this entity.
|
boolean |
hasBody()
Indicates whether this entity has a body.
|
int |
hashCode() |
String |
toString() |
public static final HttpEntity<?> EMPTY
HttpEntity
, with no body or headers.protected HttpEntity()
HttpEntity
.public HttpEntity(T body)
HttpEntity
with the given body and no headers.body
- the entity bodypublic HttpEntity(MultiValueMap<String,String> headers)
HttpEntity
with the given headers and no body.headers
- the entity headerspublic HttpEntity(T body, MultiValueMap<String,String> headers)
HttpEntity
with the given body and headers.body
- the entity bodyheaders
- the entity headerspublic HttpHeaders getHeaders()
public T getBody()
public boolean hasBody()