一、继承结构

@RequestBody、@ResponseBody的处理器:RequestResponseBodyMethodProcessor

@ModelAttribute处理器: ModelAttributeMethodProcessor

HttpEntity处理器: HttpEntityMethodProcessor

参数值解析器: HandlerMethodArgumentResolver

返回值处理器: HandlerMethodReturnValueHandler

使用HttpMessageConverter 转换方法参数: AbstractMessageConverterMethodArgumentResolver

二、 处理过程

1. RequestResponseBodyMethodProcessor的resolveArgument过程

    @Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

     //返回@RequestBody需要的参数, Spring使用我们自定义的RequestBodyAdvice, ResponseBodyAdvice发生在这里
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getGenericParameterType());
String name = Conventions.getVariableNameForParameter(parameter);

     //输入参数转换成Java对象发生在这里,后面有专门的文章讲解,注意:每次请求绑定的对象的重新生成的
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult()); return arg;
} @Override
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter methodParam,
Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException { HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

     //调用父类的readWithMessageCoverters()方法
Object arg = readWithMessageConverters(inputMessage, methodParam, paramType);
if (arg == null) {
if (methodParam.getParameterAnnotation(RequestBody.class).required()) {
throw new HttpMessageNotReadableException("Required request body is missing: " +
methodParam.getMethod().toGenericString());
}
}
return arg;
}
												

springMVC(二): @RequestBody @ResponseBody 注解实现分析的更多相关文章

  1. @RequestBody, @ResponseBody 注解详解(转)

    原文地址: https://www.cnblogs.com/qq78292959/p/3760651.html @RequestBody, @ResponseBody 注解详解(转) 引言: 接上一篇 ...

  2. SpringMVC学习八 @ResponseBody注解

    (一)在方法上只有@RequestMapping 时,无论方法返回值是什么认为需要跳转,代码实例如下 @RequestMapping("demo10") public People ...

  3. @RequestBody, @ResponseBody 注解理解

    @RequestBody, @ResponseBody 注解理解 自己以前没怎么留意过,来实习后公司采用前后端分离的开发方式,前后端拿到的注释都是 json 格式的,这时候 @RequestBody, ...

  4. @RequestBody, @ResponseBody 注解详解

    简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...

  5. SpringMVC中 解决@ResponseBody注解返回中文乱码

    问题:在前端通过get请求服务端返回String类型的服务时,会出现中文乱码问题 原因:由于spring默认对String类型的返回的编码采用的是 StringHttpMessageConverter ...

  6. SpringMVC中使用@ResponseBody注解将任意POJO对象返回值转换成json进行返回

    @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区. ...

  7. 在SpringMVC中使用@RequestBody和@ResponseBody注解处理json时,报出HTTP Status 415的解决方案

    我在使用SpringMVC的@RequestBody和@ResponseBody注解处理JSON数据的时候,总是出现415的错误,说是不支持所提交数据格式,我在页面中使用了JQuery的AJAX来发出 ...

  8. SpringMVC源码剖析5:消息转换器HttpMessageConverter与@ResponseBody注解

    转自 SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Spring源码 ...

  9. @RequestBody、@ResponseBody注解是如何将输入输出转换成json的

    @RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服务器通过交换原始文本进行通信,而这里其 ...

随机推荐

  1. Java如何拆分正则表达式和字符串?

    在Java编程中,如何拆分正则表达式和字符串? 以下示例演示如何使用regex.Pattern类的Pattern.compile()方法和patternname.split()方法拆分正则表达式. p ...

  2. Retrofit/Okhttp API接口加固技术实践(上)

    作者:Tamic 地址:http://blog.csdn.net/sk719887916/article/details/61914609 写这篇文章,我纠结了非常久,究竟是属于app安全系列,还是属 ...

  3. 年关将至业内警示P2P跑路风险

    年关将近,P2P网贷行业的问题平台亦不断增多,“跑路潮”会否再现,业内人士讨论热烈. “从历年数据统计来看,问题平台接近线性向上的增长趋势,即时间越往后,问题平台占比就越高,而每逢年关,问题平台占比都 ...

  4. python中,numeric(数字类型)和integer(整型)的区别

    说明: 在今天做int实现的过程中,官方函数的解释是将numeric转换为integer,就突然不明白,两个有啥区别. numeric-数字类型包括: int,float,bool,complex i ...

  5. Linux权限详解 命令之 chmod:修改权限

    权限简介 Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Li ...

  6. golang IO 流抽象与应用

    https://blog.csdn.net/pmlpml/article/details/82930191

  7. scala 模式匹配详解 3 模式匹配的核心功能是解构

    http://www.artima.com/scalazine/articles/pattern_matching.html这篇文章是odersky谈scala中的模式匹配的一段对话,我做了部分片段翻 ...

  8. Qt编写的开源帖子集合(懒人专用)

    回顾自己学习Qt以来九年了,在这九年多时间里面,从本论坛学习不到不少的东西,今天特意整了一下自己开源过的资源的帖子,整理一起方便大家直接跳转下载,不统计不知道,一统计吓一跳,不知不觉开源了这么多代码, ...

  9. 使用Postmark测试后端存储性能

    Postmark用于对进行频繁,大量存取小文件的存储系统的存储性能测试.原理:构建一个测试文件池,通过文件最大,最小大小,数量等参数进行配置,然后进行事务的初始化,对每一个事务中读取/附加,创建/删除 ...

  10. vmware虚拟机的tomcat启动以后,主机无法访问

    处理: 关闭防火墙服务:/etc/init.d/iptables stop ..................... 在wmware中安装linux后安装好数据库,JDK及tomcat后启动服务,虚 ...