@RequestMapping注解
1 RequestMapping源码
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String name() default "";
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
String[] consumes() default {};
String[] produces() default {};
}
1.1 @RequestMapping注解的功能
从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。
SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。
1.2 @RequestMapping注解的位置
@Target({ElementType.TYPE, ElementType.METHOD})
从源码可以看出@RequestMapping可以标识一个类(ElementType.TYPE)或者方法(ElementType.METHOD)
2 @RequestMapping注解的value和path属性
@RequestMapping注解的value/path属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value/path属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
@RequestMapping注解的value/path属性必须设置,至少通过请求地址匹配请求映射
//String[] path() default {};
//URL:http://localhost:8080/path
@RequestMapping(path = "/path")
public String testPath(){
return "index";
}
//String[] value() default {};
//URL:http://localhost:8080/firstValue;http://localhost:8080/secondValue
@RequestMapping(value = {"/firstValue", "/secondValue"})
public String testValue(){
return "index";
}
@AliasFor("path")
String[] value() default {};
@AliasFor("value")
String[] path() default {};
从源码我们可以看看出value和path是相互引用的所以这两个本质上没有区别
3 @RequestMapping注解的method属性
@RequestMapping注解的method属性通过请求的请求方式(RequestMethod)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配
多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错
405:Request method ‘POST’ not supported
注:
- 1 对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
- 处理get请求的映射–>@GetMapping
- 处理post请求的映射–>@PostMapping
- 处理put请求的映射–>@PutMapping
- 处理delete请求的映射–>@DeleteMapping
- 2 常用的请求方式有get,post,put,delete
login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body align="center">
<h1>登录</h1>
<form th:action="@{/testMethod}" method="POST">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
爱好:<input type="checkbox" name="hobby" value="angle">钓鱼
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="girl">泡妞<br>
<input type="submit" value="登录">
</form>
</body>
</html>
//RequestMethod[] method() default {};
//http://localhost:8080/testMethod
@RequestMapping(path = "/testMethod",method = RequestMethod.POST)
public String testMethod(){
return "index";
}
method 并不会改变访问路径,但是从login.html可以看出表单的提交方式必须是POST(method="POST"),否则就会报405的错误
4 @RequestMapping注解的params属性
@RequestMapping注解的params属性通过请求的请求参数匹配请求映射
@RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数
和请求映射的匹配关系
“param”:要求请求映射所匹配的请求必须携带param请求参数
“!param”:要求请求映射所匹配的请求必须不能携带param请求参数
“param=value”:要求请求映射所匹配的请求必须携带param请求参数且param=value
“param!=value”:要求请求映射所匹配的请求必须携带param请求参数但是param!=value
//key和value一对一
//String[] params() default {};
//URL http://localhost:8080/testParams?username=&password=12345
@RequestMapping(
value = "/testParams"
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username","password!=123456"}
)
public String testOneToOneParams(){
return "index";
}
//key和value一对多
//String[] params() default {};
//URL: http://localhost:8080/testParams?username=&password=123123&hobby=angle&hobby=game&hobby=girl
@RequestMapping(
value = "/testParams"
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username","password!=123456"}
)
public String testOneToMoreParams(){
return "index";
}
从上面的URL可以看出假如是多选框的话,它会被解析成多个hobby=angle&hobby=game&hobby=girl拼接,从而我们可以对其进行控制
注:若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时
页面回报错400:Parameter conditions “username, password!=123456” not met for actual
request parameters: username={admin}, password={123456}
5 @RequestMapping注解的headers属性
@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射
@RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信
息和请求映射的匹配关系
“header”:要求请求映射所匹配的请求必须携带header请求头信息
“!header”:要求请求映射所匹配的请求必须不能携带header请求头信息
“header=value”:要求请求映射所匹配的请求必须携带header请求头信息且header=value
“header!=value”:要求请求映射所匹配的请求必须携带header请求头信息且header!=value
若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面
显示404错误,即资源未找到
//String[] headers() default {};
//URL: http://localhost:8080/testHeaders
@RequestMapping(
value = "/testHeaders"
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username"}
,headers = {"Host=localhost:8080"}
)
public String testHeaders(){
return "index";
}
通过浏览器开发者工具我们可以看到request所有Head,在测试过程中我们加入了headers = {"Host=localhost:8080"},要求request的Head必须具有Host同时值还需要为localhost:8080
6 @RequestMapping注解的consumes属性
produces:它的作用是指定返回值类型,不但可以设置返回值类型还可以设定返回值的字符编码;
//produces第一种使用,返回json数据,下边的代码可以省略produces属性,因为我们已经使用了注解@responseBody就是返回值是json数据:
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
public Pet getPet(@PathVariable String petId, Model model) {
return null;
}
//produces第二种使用,返回json数据的字符编码为utf-8:
@RequestMapping(value = "/pets/{petId}", produces="MediaType.APPLICATION_JSON_VALUE";";charset=utf-8")
public Pet getPet(@PathVariable String petId, Model model) {
return null;
}
7 @RequestMapping注解的consumes属性
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) {
return "index";
}