RequestMapping与@GetMapping/@PostMapping /@PutMapping/@DeleteMapping的使用

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-张三";
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rk..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值