版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwkxq/article/details/55002608
HttpMessageConverter 主要用于处理request请求报文转java对象(@RequestBody) 和 java对象转response响应报文
此类方法列表:
//用于对@RequestBody注解的参数类型判断是否是支持mediaType格式转换
boolean canRead(Class<?> clazz, MediaType mediaType);
//用于对@ResponseBody注解的返回值类型判断是否是支持mediaType格式转换
boolean canWrite(Class<?> clazz, MediaType mediaType);
//用于返回MediaType列表供其它方法使用,一般配置了<mvc:annotion-driven/>标签就自带了常用的几个MediaType
List<MediaType> getSupportedMediaTypes();
//从请求报文中(封装成HttpInputMessage)获取请求参数,并转换成@RequestBody标注的java类型
T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException;
//用于将@ResponseBody标注的返回值类型转换成对应的响应报文(封装成HttpOutputMessage),输出浏览器
void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException;
HttpMessageConverter实际上由RequestResponseBodyMethodProcessor调用,
RequestResponseBodyMethodProcessor实现了2个接口:
HandlerMethodArgumentResolver
方法列表:
//用于判断是否使用了@RequestBody 注解
boolean supportsParameter(MethodParameter parameter);
//调用HttpMessageConverter的read方法,并做相应的处理
Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception;
HandlerMethodReturnValueHandler
方法列表:
//用于判断是否使用了@ResponseBody 注解
boolean supportsReturnType(MethodParameter returnType);
//调用HttpMessageConverter的write方法,并做相应的处理
void handleReturnValue(Object returnValue,
MethodParameter returnType,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest) throws Exception;