本文研究的一个问题就是request请求的参数是怎么来到我们的方法参数中的,以及我们的方法参数是怎么又去到response响应中的。
一. @RequestBody,@ResponseBody
@RequestBody
Annotation indicating a method parameter should be bound to the body of the web request.
The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request. Optionally, automatic validation can be
applied by annotating the argument with @Valid.
Supported for annotated handler methods in Servlet environments.
也就是说,该注解表明方法的参数和请求request 的 请求正文相绑定。请求正文会根据请求头的content-Type通过 HttpMessageConverter 对象来转换到方法参数中。有一个可选项就是我们可以通过@Valid 注解来对参数进行验证。
@ResponseBody
将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。
二. HttpMessageConverter
参考该文章,写得比较深入和具体(建议也看下它的这个系列的文章):
http://my.oschina.net/lichhao/blog/172562