请求映射
@xxxMapping;
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
Rest风格支持(使用HTTP请求方式动词来表示对资源的操作) | 以前 | 现在: /user | | —- | —- | | /getUser 获取用户 | GET-获取用户 | | /deleteUser 删除用户 | DELETE-删除用户 | | /editUser 修改用户 | PUT-修改用户 | | /saveUser保存用户 | POST-保存用户 |
核心Filter;HiddenHttpMethodFilter
1、yaml 或 properties 配置文件中开启Rest风格:
spring:
mvc:
hiddenmethod:
filter:
enabled: true #开启页面表单的Rest功能
2、form 表单中:
| <!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8”>
<title>Title</title>
</head>
<body>
<form action=”/user” method=”get”>
<input value=”REST-GET提交” type=”submit” />
</form>
_<!--POST 保存提交--><br /> _<**form action="/user" method="post"**><br /> <**input value="REST-POST提交" type="submit" **/><br /> </**form**>
_<!--DELETE 删除提交--><br /> _<**form action="/user" method="post"**><br /> <**input name="_method" type="hidden" value="DELETE"**/><br /> <**input value="REST-DELETE 提交" type="submit"**/><br /> </**form**>
_<!--PUT 修改提交--><br /> _<**form action="/user" method="post"**><br /> <**input name="_method" type="hidden" value="PUT" **/><br /> <**input value="REST-PUT提交"type="submit" **/><br /> </**form**>
</body>
</html> |
| —- |
3、Contrller层的请求映射:
@GetMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
return "GET-张三";
}
@PostMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.POST)
public String saveUser(){
return "POST-张三";
}
@PutMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String putUser(){
return "PUT-张三";
}
@DeleteMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String deleteUser(){
return "DELETE-张三";
}