概述
Spring Web
使用类HttpEntity
包装一个HTTP
请求或者响应的以下内容 : 头部和消息体。概念上来讲,可以简单理解成这样 :
1 HttpEntity = n headers + 1 body
从具体实现上来讲,可以理解成这样 :
1 HttpEntity = n HttpHeader(s) + 1 T
– 这里 T 是泛型类型,指消息体的类型
Spring Web
针对请求和响应两种情况分别继承HttpEntity
提供了实现类 :
RequestEntity
RequestEntity = HttpEntity + HttpMethod + URI + Type(消息体类型)
ResponseEntity
ResponseEntity = HttpEntity + (HttpStatus/Intgeger类型) 状态码
应用举例
HttpEntity
及其子类会被RestTemplate
或者Spring MVC
中的某个控制器方法使用。
- 例子1 : 被
RestTemplate
使用 :
// 用于表示请求消息
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);
// 用于接收响应消息
HttpEntity<String> entity = template.getForEntity("https://example.com", String.class);
String body = entity.getBody();
MediaType contentType = entity.getHeaders().getContentType();
- 例子2 : 被控制器方法使用 :
@RequestMapping("/handle")
public HttpEntity<String> handle() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new HttpEntity<String>("Hello World", responseHeaders);
}
源代码
package org.springframework.http;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
public class HttpEntity<T> {
/**
* The empty {@code HttpEntity}, with no body or headers.
*/
public static final HttpEntity<?> EMPTY = new HttpEntity<>();
private final HttpHeaders headers;
@Nullable
private final T body;
/**
* Create a new, empty {@code HttpEntity}.
*/
protected HttpEntity() {
this(null, null);
}
/**
* Create a new {@code HttpEntity} with the given body and no headers.
* @param body the entity body
*/
public HttpEntity(T body) {
this(body, null);
}
/**
* Create a new {@code HttpEntity} with the given headers and no body.
* @param headers the entity headers
*/
public HttpEntity(MultiValueMap<String, String> headers) {
this(null, headers);
}
/**
* Create a new {@code HttpEntity} with the given body and headers.
* @param body the entity body
* @param headers the entity headers
*/
public HttpEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers) {
this.body = body;
HttpHeaders tempHeaders = new HttpHeaders();
if (headers != null) {
tempHeaders.putAll(headers);
}
this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
}
/**
* Returns the headers of this entity.
*/
public HttpHeaders getHeaders() {
return this.headers;
}
/**
* Returns the body of this entity.
*/
@Nullable
public T getBody() {
return this.body;
}
/**
* Indicates whether this entity has a body.
*/
public boolean hasBody() {
return (this.body != null);
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (other == null || other.getClass() != getClass()) {
return false;
}
HttpEntity<?> otherEntity = (HttpEntity<?>) other;
return (ObjectUtils.nullSafeEquals(this.headers, otherEntity.headers) &&
ObjectUtils.nullSafeEquals(this.body, otherEntity.body));
}
@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(this.headers) * 29 + ObjectUtils.nullSafeHashCode(this.body));
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("<");
if (this.body != null) {
builder.append(this.body);
builder.append(',');
}
builder.append(this.headers);
builder.append('>');
return builder.toString();
}
}