展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

Spring MVC入门(二):@RequestMapping注解

  • @RequestMapping简介
@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系
SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息
  • value属性
@RequestMapping注解的value属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址
所对应的请求
@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

# case
@RequestMapping(
    value = {"/testRequestMapping", "/test"}
)
  • method属性
@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配
多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错
405:Request method 'POST' not supported

# case
@RequestMapping(
    value = {"/testRequestMapping", "/test"},
    method = {RequestMethod.GET, RequestMethod.POST}
)
  • @RequestMapping的派生注解
处理get请求的映射-->@GetMapping
处理post请求的映射-->@PostMapping
处理put请求的映射-->@PutMapping
处理delete请求的映射-->@DeleteMapping
  • params属性
@RequestMapping注解的params属性通过请求的请求参数匹配请求映射
@RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数
和请求映射的匹配关系
"param":要求请求映射所匹配的请求必须携带param请求参数
"!param":要求请求映射所匹配的请求必须不能携带param请求参数
"param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value
"param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value

# case: 参数中必须包括username,且password不能为123456
@RequestMapping(
value = {
    "/testRequestMapping", "/test"}
    ,method = {RequestMethod.GET, RequestMethod.POST}
    ,params = {"username","password!=123456"}
)
  • headers属性
@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射
@RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信
息和请求映射的匹配关系
"header":要求请求映射所匹配的请求必须携带header请求头信息
"!header":要求请求映射所匹配的请求必须不能携带header请求头信息
"header=value":要求请求映射所匹配的请求必须携带header请求头信息且header=value
"header!=value":要求请求映射所匹配的请求必须携带header请求头信息且header!=value
若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面
显示404错误,即资源未找到
  • 查看请求头中的属性
# case: 例如要求请求头中的属性Host必须为localhost:8081
@RequestMapping(
value = {
    "/testRequestMapping", "/test"}
    ,method = {RequestMethod.GET, RequestMethod.POST}
    ,params = {"username","password!=123456"}
    ,headers = {"Host=localhost:8081"}
)
  • @equestMappingant风格的路径
# 例如如下配置
@RequestMapping("/a?a/test")
# 前端可使用如下方式访问(?可匹配任意单个字符,必须匹配一个字符)
http://localhost:8080/ada/test
# 不能匹配空,不能匹配多个,不能匹配?,不能匹配/
http://localhost:8080/aa/test
http://localhost:8080/adda/test
http://localhost:8080/a?a/test
http://localhost:8080/a/a/test

*:表示任意的0个或多个字符
@RequestMapping("/a*a/test")
# 可以使用如下方式
http://localhost:8080/aa/test
http://localhost:8080/adda/test
# 不能使用如下方式
http://localhost:8080/a?a/test
http://localhost:8080/a/a/test

**:表示任意的一层或多层目录
@RequestMapping("/a**a/test")
# 可以使用如下方式
http://localhost:8080/aa/test
http://localhost:8080/adda/test
# 不能使用如下方式
http://localhost:8080/a?a/test
http://localhost:8080/a/a/test

@RequestMapping("/**/test")
# 可以使用如下方式
http://localhost:8080/a/a/test
http://localhost:8080/a/a/a/test
  • 占位符
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
    System.out.println("id:"+id+", username:"+username);
    return "success";
}

# http://localhost:8080/testRest/1/goudan
posted @ 2022-04-21 15:40  DogLeftover  阅读(37)  评论(0编辑  收藏  举报