一,@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 {@link 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 {@code @Valid}
即:此注解表明方法参数应该和请求体绑定,换句话说就是@RequestBody接收的是请求体中的参数(当是post请求时,数据就会在请求体中),在接收参数的过程中HttpMessageConverter会根据请求的contentType值去解析方法参数,还可以有选择的给参数加一些验证。因此,这个注解需要注意的点就是,ajax中contentType值的设置与Controller中对应方法的参数类型要对应,一般来说,如果Controller中是实体接收,那么contentType值要设为application/json。(ps:请求体中的数据是通过流获取的噢~)
二,@RequestParam
官方注释为:
Annotation which indicates that a method parameter should be bound to a web
* request parameter.
......
即:方法参数和请求参数绑定,在SpringMVC中,这个请求参数指的是,query parameters, form data,
and parts in multipart requests。
- query parammeters:如果是get请求,那么参数都是以query parameters的形式存在
- form data:post请求的一般表单数据,例如,当contentType=application/x-www-form-urlencoded时
- multipart requests:还不太了解(后续知道了会重新更新)
@RequestParam接收的是以上三种形式的参数。
三,@PathVariable
官方注释为:
Annotation which indicates that a method parameter should be bound to a URI template
* variable. Supported for {@link RequestMapping} annotated handler methods.
即:方法参数与URI模板变量绑定,与@RequestMapping一起,形如: