Spring Web : 概念模型 HttpEntity

本文深入解析了SpringWeb中HttpEntity类的使用方法和内部结构,包括其如何封装HTTP请求和响应的头部和消息体,以及RequestEntity和ResponseEntity的具体实现。通过实例展示了HttpEntity在RestTemplate和SpringMVC中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

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();
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值