区别:
@GetMapping 用于HTTP的GET请求;
@PostMapping 用于HTTP的POST请求;
@RequestMapping 具有类属性,可以进行GET,POST,PUT或其他的注释中具有的请求方法。
具体来说:
@GetMapping 组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写
@PostMapping 组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写
@GetMapping 和 @PostMapping 二者都是@RequestMapping的一个延伸,目的就是为了提高清晰度。
简单示例:
@GetMapping演示:
controller层代码
@GetMapping("/get")
@ResponseBody
public String get(){
return "get000";
}
html使用get请求
浏览器访问
使用@GetMapping,若修改为post提交,则会报错405
---------------------------------------------------------------------------------------------------------------------------------
@RequestMapping 演示:
修改Controller层的@GetMapping为@RequestMapping
// @GetMapping("/get")
@RequestMapping("/get")
@ResponseBody
public String get(){
return "get000";
}
使用GET或POST都能够正常跳转。
若定义中的method方法则需要与浏览器提交的方式一致
//get
@RequestMapping(value = "/get",method = RequestMethod.GET)
//post
@RequestMapping(value = "/get",method = RequestMethod.POST)