- @RequestMapping的定义
- @RequestMapping注解的作用
- @RequestMapping注解的位置
- @RequestMapping的属性
- value属性
- method属性
- params属性
- headers属性
本篇示例基于第一个SpringMVC程序。
@RequestMapping的定义//RequestMapping.class
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 {};
}
@RequestMapping注解的作用
@RequestMapping的作用是,将请求、处理请求的控制器方法关联起来,建立映射关系。
@Target({ElementType.TYPE, ElementType.METHOD})
@RequestMapping注解既可以标识在类上(ElementType.TYPE),也可以标识在方法(ElementType.METHOD)。比如,
@Controller
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/hello")
public String testHello(){
return "hello";
}
}
Hello
Here is hello page
此时,控制器方法testHello()映射的请求路径为/test/hello。
RequestMapping有如下属性:
- name():String
- value():String[]
- path():String[]
- method():RequestMethod[]
- params():String[]
- headers():String[]
- consumers():String[]
- produces():String[]
@RequestMapping(value=xxx),通过请求地址匹配请求。
value():String[],即 value属性值既可以是一个字符串,也可以是一个字符串数组。
@Controller
public class TestController {
@RequestMapping(value = {"/test","/test/value"})
public String testValue(){
return "success";
}
}
success
请求成功!
示例中,@RequestMapping(value = {"/test","/test/value"}),value属性值是一个字符串数组,表示同时匹配多个请求地址:/test和/test/value。
@RequestMapping(value = {"/test","/test/value"})同@RequestMapping({"/test","/test/value"})。
另外,关于请求地址的匹配,以下两点需要注意。
第一点:ant风格中的通配符
ant风格,是请求地址的一种匹配方式,用于模糊匹配,有如下3种通配符,
- ?,用于匹配单个字符。比如@RequestMapping("/a?a/test"),可匹配/aaa/test、/aba/test,/a1a/test等请求地址。
- *,用于匹配零个或多个字符。比如@RequestMapping("/a*a/test"),可匹配/aa/test、/aca/test、/abcdefa/test等请求地址。
- **,用于匹配一层或多层路径,用于@RequestMapping("xxxx")方式。比如test,可匹配/test、/user/test、/a/b/c/d/test等请求地址。
第二点:rest风格中的占位符
请求路径上的占位符{xxx},常见于restful风格中。
把要传输给服务器的数据放到请求路径的占位符上,如@RequestMapping("/test/{id}")。
使用@PathVariable注解,可将占位符表示的数据赋值给控制器方法的形参。
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/test/{id}")
public String test(@PathVariable("id") Long id){
System.out.println("id:"+id);
return "success";
}
}
method属性
@RequestMapping(method=xxx),通过请求方法匹配请求。
method():RequestMethod[],method属性值既可以是一个RequestMethod类型的方法,也可以是RequestMethod类型的数组。
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
return "success";
}
@RequestMapping(value = "/target",method = {RequestMethod.GET,RequestMethod.POST})
public String target(){
return "target";
}
}
首页
Hello World!
success
here is success
target
here is target
示例中,@RequestMapping(value = "/test",method = RequestMethod.GET),请求路径匹配/test,请求方法匹配get方法;
@RequestMapping(value = "/target",method = {RequestMethod.GET,RequestMethod.POST}),请求路径匹配/target,可匹配多个请求方法:get和post。
常用的请求方法有:get、post、put和delete,针对这些方法,Spring提供了@RequestMapping对应的派生注解,如下,
- @GetMapping,同@RequestMapping(method = RequestMethod.GET)
- @PostMapping,同@RequestMapping(method = RequestMethod.POST)
- @PutMapping,同@RequestMapping(method = RequestMethod.PUT)
- @DeleteMapping,同@RequestMapping(method = RequestMethod.DELETE)
@RequestMapping(params=xxx),通过请求参数匹配请求。
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
@GetMapping(value = "/test",params = {"username"})
public String test(){
return "success";
}
@GetMapping(value = "/test2",params = {"!username"})
public String test2(){
return "success";
}
@GetMapping(value = "/test3",params = {"username=tom"})
public String test3(){
return "success";
}
@GetMapping(value = "/test4",params = {"username!=tom"})
public String test4(){
return "success";
}
}
首页
Hello World!
@{/test(username='tom')}<-->@GetMapping(value = "/test",params = {"username"}),测试:Yes
@{/test}<-->@GetMapping(value = "/test",params = {"username"}),测试:No
@{/test2}<--->@GetMapping(value = "/test2",params = {"!username"}),测试2:Yes
@{/test2(username='tom')}<--->@GetMapping(value = "/test2",params = {"!username"}),测试2:No
@{/test3(username='tom')}<--->@GetMapping(value = "/test3",params = {"username=tom"}),测试3:Yes
@{/test3(username='jack')}<--->@GetMapping(value = "/test3",params = {"username=tom"}),测试3:No
@{/test4(username='jack')}<--->@GetMapping(value = "/test4",params = {"username!=tom"}),测试4:Yes
@{/test4(username='tom')}<--->@GetMapping(value = "/test4",params = {"username!=tom"}),测试4:No
success
here is success
- params = {"username"},请求参数必须携带username。
- params = {"!username"},请求参数不能携带username。
- params = {"username=tom"},请求参数必须携带username,且username的值是tom。
- params = {"username!=tom"},请求参数必须携带username,且username的值不能是tom。
如果需要匹配多个请求参数,将params设置为字符串数组即可,如params = {"username","password"}。
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
@GetMapping(value = "/test",params = {"username","password"})
public String test(){
return "success";
}
}
首页
Hello World!
测试
headers属性
@RequestMapping(headers=xxx),通过请求头匹配请求。
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
@GetMapping(value = "/test",headers = {"Host"})
public String test(){
return "success";
}
@GetMapping(value = "/test2",headers = {"!Host"})
public String test2(){
return "success";
}
@GetMapping(value = "/test3",headers = {"Host=localhost:8080"})
public String test3(){
return "success";
}
@GetMapping(value = "/test4",headers = {"Host!=localhost:8080"})
public String test4(){
return "success";
}
}
首页
Hello World!
测试
测试2
测试3
测试4
success
here is success
- headers = {"Host"},表示请求头信息必须携带Host。
- headers = {"!Host"},表示请求头信息不能携带Host。
- headers = {"Host=localhost:8080"},表示请求头信息必须携带Host,且Host的值必须为localhost:8080。
- headers = {"Host!=localhost:8080"},表示请求头信息必须携带Host,且Host的值不能是localhost:8080。



