【Spring】抽丝剥茧SpringMVC-HttpMessageConverter

本文源码基于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



目录 目录

上一篇 ContentNegotiationManager

下一篇 请求间传递参数机制FlashMap

再下一篇 SessionAttributes机制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值