Package org.springframework.http

# Class HttpEntity<T>

java.lang.Object
org.springframework.http.HttpEntity<T>

Type Parameters:
  `T` - the body type

Direct Known Subclasses:
  `RequestEntity`, `ResponseEntity`

public class HttpEntity<T> extends Object
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:
  - `RestTemplate`
  - `getBody()`
  - `getHeaders()`

## Field Summary

Fields
Modifier and Type
Field
Description
`static final HttpEntity<?>`
`EMPTY`
An `HttpEntity` instance with a `null` body and `empty headers`.

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`HttpEntity()`
Create a new, empty `HttpEntity`.
``
`HttpEntity(org.springframework.util.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, org.springframework.util.MultiValueMap<String,String> headers)`
Create a new `HttpEntity` with the given body and headers.

## Method Summary

Modifier and Type
Method
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()`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Field Details

### EMPTY

public static final HttpEntity<?> EMPTY
An `HttpEntity` instance with a `null` body and `empty headers`.

## Constructor Details

### HttpEntity

protected HttpEntity()
Create a new, empty `HttpEntity`.

### HttpEntity

public HttpEntity(T body)
Create a new `HttpEntity` with the given body and no headers.

Parameters:
  `body` - the entity body

### HttpEntity

public HttpEntity(org.springframework.util.MultiValueMap<String,String> headers)
Create a new `HttpEntity` with the given headers and no body.

Parameters:
  `headers` - the entity headers

### HttpEntity

public HttpEntity(@Nullable T body, @Nullable org.springframework.util.MultiValueMap<String,String> headers)
Create a new `HttpEntity` with the given body and headers.

Parameters:
  `body` - the entity body
  `headers` - the entity headers

## Method Details

### getHeaders

public HttpHeaders getHeaders()
Returns the headers of this entity.

### getBody

@Nullable public T getBody()
Returns the body of this entity.

### hasBody

public boolean hasBody()
Indicates whether this entity has a body.

### equals

public boolean equals(@Nullable Object other)

Overrides:
  `equals` in class `Object`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`

### toString

public String toString()

Overrides:
  `toString` in class `Object`