本文源码基于SpringMVC 5.2.7
HttpMessageConverter
HttpMessageConverter是SpringMVC重要功能组件之一。它提供两个基础功能:其一,从request body中读取数据并转换为对应的类型;其二,将对象写入到response body中。HttpMessageConverter是接口,规范了具体实现类的行为。HttpMessageConverter主要提供两组接口分别对应读、写。
HttpMessageConverter主要在参数解析、返回值处理两个场景中使用,例如参数解析器RequestResponseBodyMethodProcessor(即是参数解析器也是返回处理器)、HttpEntityMethodProcessor等,返回处理器ResponseBodyEmitterReturnValueHandler等。
public interface HttpMessageConverter<T> {
//笔者注:判断是否能从输入流读取并返回指定类型的对象
//参数clazz:Class<?>类型,指定的类型
//参数mediaType:MediaType类型,request的Content-Type
boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
//笔者注:判断是否能将指定类型的对象写入到输出流
//参数clazz:Class<?>类型,指定的类型
//参数mediaType:MediaType类型,写到response的"Content-Type"
boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);
List<MediaType> getSupportedMediaTypes();
//笔者注:从输入流中读取并返回指定类型的对象
//参数clazz:Class<?>类型,指定的类型
//参数inputMessage:HttpInputMessage类型,输入流
T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException;
//笔者注:将指定对象写入到输出流
//参数t:泛型T,将被写到输出流的对象
//参数contentType:MediaType类型,写到response的"Content-Type",与canWrite对应
//参数outputMessage:HttpOutputMessage类型,输出流
void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException;
}
GenericHttpMessageConverter
GenericHttpMessageConverter是HttpMessageConverter子接口,增加了对泛型的支持
public interface GenericHttpMessageConverter<T> extends HttpMessageConverter<T> {
boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType);
T read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException;
boolean canWrite(@Nullable Type type, Class<?> clazz, @Nullable MediaType mediaType);
void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException;
}
常见实现类
实现类 | 功能 | 支持的条件 | 是否默认添加 |
---|---|---|---|
org.springframework.http.converter.ByteArrayHttpMessageConverter | 读取写入byte数组 | 指定类型是byte[] MediaType是application/octet-stream、*/* | 是 |
org.springframework.http.converter.StringHttpMessageConverter | 读取写入String类型对象 | 指定类型是String MediaType是text/plain、*/* | 是 |
org.springframework.http.converter.xml.SourceHttpMessageConverter | 读取写入javax.xml.transform.Source类型对象 | 指定类型 javax.xml.transform.dom.DOMSource、javax.xml.transform.sax.SAXSource、 javax.xml.transform.stax.StAXSource、javax.xml.transform.stream.StreamSource或接口javax.xml.transform.Source MediaType是application/xml、text/xml、application/*+xml | 是 |
org.springframework.http.converter.support .AllEncompassingFormHttpMessageConverter | 读取写入org.springframework.util.MultiValueMap类型对象 | 指定类型org.springframework.util.MultiValueMap及子类 读取支持的MediaType是application/x-www-form-urlencoded 写入支持的MediaType是application/x-www-form-urlencoded、multipart/form-data、multipart/mixed | 是 |
org.springframework.http.converter.json .MappingJackson2HttpMessageConverter | 读取写入任何对象类型 | 无需指定类型 MediaType是application/json、application*+json | 否 |
目录 目录
再下一篇 SessionAttributes机制