@RequestMapping表明有六个属性:
下面进行详细的讲解:
备注:

@RequestMapping中consumes和produces的差异RequestMapping中consumes和produces的差异:
说到这两个参数,不得不先回顾一下HTTP协议Header中的两个东西
Content-Type 和Accept。在Request中,Content-Type 用来见告做事器当前发送的数据是什么格式;而Accept 用来见告做事器,客户端能认识哪些格式数据,最好返回这些格式中的个中一种。
consumes 用来限定Content-Type produces 用来限定Accept例子:
有个用户给发了一个要求,
要求头中
Content-Type =application/json
Accept = /
便是说用户发送的json格式的数据,可以吸收任意格式的数据返回。但是如果接口中定义如下:
@Controllerpublic class HelloWorld { @RequestMapping(value=\公众/helloworld\"大众,consumes={\"大众application/xml\"大众},produces={\"大众application/xml\"大众}) public String hello(){ System.out.println(\"大众hello world\"大众);return\公众success\"大众; }}
该接口只吸收 application/xml 格式数据,也只返回application/xml格式数据。很明显是调不通这个接口的。
轻微改一下该接口,即可:
@Controllerpublic class HelloWorld { @RequestMapping(value=\公众/helloworld\"大众,consumes={\公众application/xml\公众,\"大众application/json\"大众},produces={\公众application/xml\"大众}) public String hello(){ System.out.println(\公众hello world\"大众);return\"大众success\公众; }}
handler method参数绑定常用的表明,根据他们处理的request的不同内容部分分为四类:
处理request uri部分的表明:@PathVariable;处理request header部分的表明:@RequestHeader, @CookieValue;处理request body部分的表明:@RequestParam, @RequestBody;处理attribute类型的表明:@SessionAttributes, @ModelAttribute;@PathVariable
当利用@RequestMapping URI template样式映射时,即someUrl/{paramId},这时的paramId可通过@PathVariable表明绑定它传过来的值到方法的参数上。
@RequestHeader、@CookieValue
@RequestHeader表明可以把Request要求header部分的值绑定到方法的参数上。
@CookieValue可以把Request header中关于cookie的值绑定到方法的参数上。