Often used in combination with the org.,
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);
}
org.springframework.web.client.RestTemplate , getBody(), getHeaders()
| Modifier and Type | Field and Description |
|---|---|
| private final T | |
| public static final HttpEntity | EMPTY
The empty |
| private final HttpHeaders |
| Access | Constructor and Description |
|---|---|
| protected | |
| public | |
| public | HttpEntity(MultiValueMap<String, String>
the entity headers headers)Create a new |
| public | HttpEntity(T
the entity body body, MultiValueMap<String, String> the entity headers headers)Create a new |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. other)Overrides java. Indicates whether some other object is "equal to" this one. |
| public T | |
| public HttpHeaders | |
| public boolean | |
| public int | |
| public String |
| body | back to summary |
|---|---|
| private final T body
| |
| EMPTY | back to summary |
|---|---|
| public static final HttpEntity<?> EMPTY The empty | |
| headers | back to summary |
|---|---|
| private final HttpHeaders headers | |
| HttpEntity | back to summary |
|---|---|
| protected HttpEntity() Create a new, empty | |
| HttpEntity | back to summary |
|---|---|
| public HttpEntity(T body) Create a new
| |
| HttpEntity | back to summary |
|---|---|
| public HttpEntity(MultiValueMap<String, String> headers) Create a new
| |
| HttpEntity | back to summary |
|---|---|
| public HttpEntity(T body, MultiValueMap<String, String> headers) Create a new
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| getBody | back to summary |
|---|---|
| public T getBody() Returns the body of this entity.
| |
| getHeaders | back to summary |
|---|---|
| public HttpHeaders getHeaders() Returns the headers of this entity. | |
| hasBody | back to summary |
|---|---|
| public boolean hasBody() Indicates whether this entity has a body. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |