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
还是用到了另外两个接口 HttpInputMessage
和HttpOutputMessage
:
- 接口
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
消息体
- 表示一个