学无先后,达者为师

网站首页 编程语言 正文

【IDEA】@RequestMapping与@GetMapping、@PostMapping的区别

作者:峰子的番茄肥牛 更新时间: 2024-03-22 编程语言

区别:

@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)

原文链接:https://blog.csdn.net/weixin_63453779/article/details/131605701

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新