RequestMapping:该注解用于处理请求地址的映射
参考自: https://blog.csdn.net/qq_33186251/article/details/54599694 和 https://www.cnblogs.com/kuoAT/p/7121753.htm l
package:import org.springframework.web.bind.annotation.RequestMapping;
属于jar: org/springframework/spring-web/4.3.19.RELEASE/spring-web-4.3.19.RELEASE-sources.jar
一、RequestMapping级别:
1.类级别(Type-level),注释在类的上面
2.方法级别(Method-level),注释在方法上面
二、RequestMapping属性
1.value:指定请求的实际地址
(1)默认:@RequestMapping("/uri") 等同于 @RequestMapping(value="/uri")
(2)@RequestMapping("/uri"):其中uri取值可以分为3种:
①为普通的具体字符串
1 @RequestMapping(value="/hello") 2 public String hello() { 3 return "你好!世界!"; 4 }
②为含有某变量的一类值
1 @RequestMapping(value="/findUser/{userId}", method=RequestMethod.GET) 2 public String findUser(@PathVariable String userId, Model model) { 3 return "my User:"+userId; 4 }
③为含正则表达式的一类值
1 @RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\d]+}") 2 public String regularExpression( 3 @PathVariable String textualPart, 4 @PathVariable String numericPart){ 5 6 System.out.println("Textual part: " + textualPart + 7 ", numeric part: " + numericPart); 8 return "someResult"; 9 }
2.method:指定请求的method类型,GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE等;
(1)@RequestMapping(value="/uri",method=RequestMethod.POST)
(2)支持定义在类级别或方法级别。
(3)可以通过HTTP请求的method来缩小主映射的范围。
3.consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
1 @Controller 2 @RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json") 3 public void addPet(@RequestBody Pet pet, Model model) { 4 // implementation omitted 5 }
方法仅处理request Content-Type为“application/json”类型的请求。
4.produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
1 @Controller 2 @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json") 3 @ResponseBody 4 public Pet getPet(@PathVariable String petId, Model model) { 5 // implementation omitted 6 }
方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;
5.params: 指定request中必须包含某些参数值时,才让该方法处理。
(1)@RequestMapping(value="/uri",params="myParam")
(2)支持定义在类级别或方法级别
(3)通过映射请求的参数来缩小主映射的范围。
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}
仅处理请求中包含了名为“myParam”,值为“myValue”的请求;
6.headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。headers:也支持(*)
(1)@RequestMapping(value = "/uri", headers = "content-type=text/*")
(2)通过请求的header来缩小主映射的范围。
1 @Controller 2 @RequestMapping("/owners/{ownerId}") 3 public class RelativePathUriTemplateController { 4 5 @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/") 6 public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { 7 // implementation omitted 8 } 9 }
仅处理request的header中包含了指定“Refer”请求头和对应值为“http://www.ifeng.com/
”的请求;