@RequestMapping
参考: https://www.oschina.net/translate/using-the-spring-requestmapping-annotation
这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。
注:path和value这两个属性是一样的,用于指定HTTP请求资源(URI)的实际映射地址,当不指定具体的属性值时,默认是给该属性传递值
处理多个URI
只需要用逗号隔开每个域,支持通配符和正则表达式
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = {
"",
"/page",
"page*",
"view/*,**/msg"
})
String indexMultipleMapping() {
return "Hello from index multiple mapping.";
}
}
view/*表示/home/view/下的所有请求
**/msg表示处理所有/home/…/msg的请求
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
String getDynamicUriValue(@PathVariable String id) {
System.out.println("ID is " + id);
return "Dynamic URI parameter fetched";
}
@RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
String getDynamicUriValueRegex(@PathVariable("name") String name) {
System.out.println("Name is " + name);
return "Dynamic URI parameter fetched using regex";
}
}
带有 @RequestParam 的 @RequestMapping
@RequestParam 注解配合 @RequestMapping 一起使用,可以将请求的参数同处理方法的参数绑定在一起。
@RequestParam 注解使用的时候可以有一个值,也可以没有值。这个值指定了需要被映射到处理方法参数的请求参数, 代码如下所示:
@RequestMapping(value= {"person", "stu"})
public String person(@RequestParam(value="person", required=false) String person_name, @RequestParam(value="stu", required=false) String stu_name) {
if(person_name!=null)
return "person name:" + person_name;
else if(stu_name!=null)
return "stu name:" + stu_name;
else
return "no name";
}
required=true则必须传入参数,否则会报错
用 @RequestMapping 处理 HTTP 的各种方法
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(method = RequestMethod.GET)
String get() {
return "Hello from get";
}
@RequestMapping(method = RequestMethod.DELETE)
String delete() {
return "Hello from delete";
}
@RequestMapping(method = RequestMethod.POST)
String post() {
return "Hello from post";
}
@RequestMapping(method = RequestMethod.PUT)
String put() {
return "Hello from put";
}
@RequestMapping(method = RequestMethod.PATCH)
String patch() {
return "Hello from patch";
}
}
用 @RequestMapping 来处理生产和消费对象
与并发中的生产者消费者不同,这里主要指的是
- 生产者产生设定格式的返回对象
- 消费者只处理设定格式的请求
@RequestMapping(value="/prod",produces={"application/JSON"})
public String getProduces(){
return "Produces attribute";
}
@RequestMapping(value="/cons",consumes={"application/JSON", "application/XML"})
String getConsumes(){
return "Consumes attribute";
}
在这段代码中, getProduces() 处理方法会产生一个 JSON 响应, getConsumes() 处理方法可以同时处理请求中的 JSON 和 XML 内容。
使用 @RequestMapping 来处理消息头
header 元素来根据请求中的消息头内容缩小请求映射的范围。
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/head", headers = {
"content-type=text/plain"
})
String post() {
return "Mapping applied along with headers";
}
}
//处理多个请求头
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/head", headers = {
"content-type=text/plain",
"content-type=text/html"
}) String post() {
return "Mapping applied along with headers";
}
}
使用 @RequestMapping 来处理请求参数
params 元素可以进一步帮助我们缩小请求映射的定位范围。使用 params 元素,你可以让多个处理方法处理到同一个URL 的请求, 而这些请求的参数是不一样的。
你可以用 myParams = myValue 这种格式来定义参数,不支持通配符
//只支持personId=20的请求
@RequestMapping(value="/fetch",params={"personId=20"})
String getParamsDifferent(@RequestParam("personId") String id){
return "Fetched parameter using params attribute = " + id;
}
@RequestMapping 默认的处理方法
@RequestMapping 默认的处理方法
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping()
String
default () {
return "This is a default method for the class";
}
}
@RequestMapping 快捷方式
@RequestMapping 的组合注解。组合注解可以更好的表达被注解方法的语义。它们所扮演的角色就是针对 @RequestMapping 的封装,而且成了定义端点的标准方法。
- @GetMapping=@RequestMapping(method =RequestMethod.GET)
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping