springmvc HttpMessageConverter(http消息转换器)

来源:互联网 发布:js增加表格指定行 编辑:程序博客网 时间:2024/06/12 14:09

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/>标签就自带了常用的几个MediaTypeList<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;



0 0
原创粉丝点击