RequestMapping注解有两个属性:
value, method:
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
查询:
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写
@RequestMapping(value = "/findAllUser",method = RequestMethod.GET),等于@GetMapping(value = "/findAllUser")
增加:
@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写
@RequestMapping(value = "/addUser",method = RequestMethod.POST) ,等于@PostMapping(value = "/addUser")
修改:
@PutMapping是一个组合注解,是@RequestMapping(method = RequestMethod.PUT)的缩写
@RequestMapping(value = "/updateUser",method = RequestMethod.PUT) 等于@PutMapping(value = "/updateUser")
删除:
@DeleteMapping是一个组合注解,是@RequestMapping(method = RequestMethod.DELETE)的缩写
@RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE) 等于@DeleteMapping(value = "/deleteUser")
前端表单提交只有get和post两种方式:
在Springboot中开启Rest风格:
spring:
mvc:
hiddenmethod:
filter:
enabled: true #开启页面表单的Rest功能
前端页面表单和后端Controller:
<body>
测试REST风格
<form action="/user" method="post">
<input value="REST-POST 提交" type="submit">
</form>
<form action="/user" method="get">
<input value="REST-GET 提交" type="submit">
</form>
<form action="/user" method="post">
<input name="_method" type="hidden" value="PUT">
<input value="REST-PUT 提交" type="submit">
</form>
<form action="/user" method="post">
<input name="_method" type="hidden" value="DELETE">
<input value="REST-DELETE 提交" type="submit">
</form>
</body>
PUT和DELETE请求:表单提交会带上真正的提交方式参数:_methond=PUT
@RestController
public class restController {
@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
return "GET-张三";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String saveUser(){
return "POST-张三";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String putUser(){
return "PUT-张三";
}
@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String deleteUser(){
return "DELETE-张三";
}
}