Spring : 概念模型接口 HttpMessageConverter

Spring框架中,HttpMessageConverter是一个策略接口,用于定义从HTTP请求读取消息或者往HTTP响应中写入消息的消息转换工具。该接口有不同的实现。不同的实现逻辑体现了不同的"转换("converter)语义。每个实现可以仅仅支持从HTTP请求读取消息,或者仅仅支持往HTTP响应中写入消息,或者二者都支持。

接口HttpMessageConverter源代码

package org.springframework.http.converter;

import java.io.IOException;
import java.util.List;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;

/**
 * Strategy interface that specifies a converter that can convert from and 
 * to HTTP requests and responses.
 * 策略接口,指定一个能够对HTTP request, response进行转换的转化器。
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 3.0
 */
public interface HttpMessageConverter<T> {

	/**
	 * Indicates whether the given class can be read by this converter.
	 * 指出给定的类是否能被当前转换器读取。
	 * @param clazz the class to test for readability
	 * @param mediaType the media type to read (can be null if not specified);
	 * typically the value of a Content-Type header.
	 * @return 可读的话返回true,其他情况返回 false
	 */
	boolean canRead(Class<?> clazz, MediaType mediaType);

	/**
	 * Indicates whether the given class can be written by this converter.
	 * 指出给定的类是否能被当前转换器写操作。
	 * @param clazz the class to test for writability
	 * @param mediaType the media type to write (can be null if not specified);
	 * typically the value of an Accept header.
	 * @return 可写的话返回true,其他情况返回 false
	 */
	boolean canWrite(Class<?> clazz, MediaType mediaType);

	/**
	 * Return the list of MediaType objects supported by this converter.
	 * 返回该转换器支持的 MediaType 对象的列表。
	 * @return the list of supported media types
	 */
	List<MediaType> getSupportedMediaTypes();

	/**
	 * 从输入消息 inputMessage 中读取指定类型 clazz 的一个对象并返回它。
     * @param clazz 要读取等消息的类型,如果把该类传给当前转换器的的 canRead方法,
     * 其返回值应该是 true
	 * @param inputMessage the HTTP input message to read from
	 * @return the converted object
	 * @throws IOException in case of I/O errors
	 * @throws HttpMessageNotReadableException in case of conversion errors
	 */
	T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException;

	/**
	 * Write an given object to the given output message.
	 * 写入一个对象到给定的输出消息。
	 * @param t 要写入到输出消息的对象.这个对象的类型传递给当前转换器的canWrite方法的话,
	 * 返回值应该是 true。 
	 * @param contentType 写入对象所要使用的 content type. 可以是 null , 表示使用转换器
	 * 缺省 content type 。如果不是 null, 该参数必须是一个传给当前转换器的 canWrite 函数时
	 * 返回 true 的对象。
	 * @param outputMessage the message to write to
	 * @throws IOException in case of I/O errors
	 * @throws HttpMessageNotWritableException in case of conversion errors
	 */
	void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException;

}

HttpMessageConverter使用到的其它接口

HttpMessageConverter还是用到了另外两个接口 HttpInputMessageHttpOutputMessage

  • 接口 HttpInputMessage
    • 表示一个 HTTP 输入消息
    • 通常在客户端用于处理一个 HTTP 响应,在服务端用于处理一个 HTTP请求
    • 带有两个方法
      • HttpHeaders getHeaders() – 返回 HTTP 消息的头部,不会是 null
      • InputStream getBody() throws IOException – 返回 HTTP 消息体
  • 接口 HttpOutputMessage
    • 表示一个 HTTP 输出消息
    • 通常在客户端用于处理一个 HTTP 请求,在服务端用于处理一个HTTP响应
    • 带有两个方法
      • HttpHeaders getHeaders() – 返回 HTTP 消息的头部,不会是 null
      • InputStream getBody() throws IOException – 返回HTTP消息体

HttpMessageConverter的类层级结构

HttpMessageConverter的类层级结构

HttpMessageConverter及其常见的一些实现类

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值