@DeleteMapping和@PostMapping和@GetMapping和Content-Type使用记录

@RestController
@RequestMapping(value = "demo")
public class TestController {

    //Content-Type:application/x-www-form-urlencoded;表单提交form-data
    @PostMapping("/demo1")
    public String test1(@RequestParam("id") Integer id,@RequestParam("name") String name) {
        System.out.println("test1......");
        return Integer.valueOf(id) + ":" + name;
    }

    //Content-Type:application/x-www-form-urlencoded
    @PostMapping("/demo2")
    public String test2(DemoUser demoUser) {
        System.out.println("test2......");
        return Integer.valueOf(demoUser.getId()) + ":" + demoUser.getName();
    }
    //Content-Type:application/json
    @PostMapping("/demo3")
    public String test3(@RequestBody DemoUser demoUser) {
        System.out.println("test3......");
        return Integer.valueOf(demoUser.getId()) + ":" + demoUser.getName();
    }

    //Content-Type:application/x-www-form-urlencoded;form-data表单;application/json(用@RequestBody接收)都可以
    @DeleteMapping("/demo4")
    public String test4(@RequestParam("id") Integer id,@RequestParam("name") String name) {
        System.out.println("test4......");
        return Integer.valueOf(id) + ":" + name;
    }

    //http:localhost:8080/demo/1
    @GetMapping("/{id}")
    public String test5(@PathVariable("id") Integer id) {
        System.out.println("test5....");
        return String.valueOf(id);
    }
     //application/json
    @PostMapping("/demo6")
    public String test6(@RequestBody Map<String,Object> map) {
        System.out.println("test6...............");
        return map.get("id") + ":" + map.get("name");
    }
}

以上是示例代码,总结如下

content-type,表示你前端用那种方式传参。
如果application/json使用json传参,那么你后台就需要用@RequstBody来接受参数。
如果用form-data表单方式传参,那么你后台可以直接用一个vo对象接收,或者你直接使用@RequestParam参数来接受
最后一个例子中,http:localhost:8080/demo/{id}–这种方式传参,你需要@PathVariable来接受参数
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RequestMapping注解是Spring MVC框架中的一个核心注解,用于映射请求路径和处理方法。它可以应用在类级别和方法级别,用来指定请求的URL路径、请求方法、请求头等信息。 @RequestMapping注解的参数规范如下: 1. value:指定请求的URL路径,可以是一个字符串或字符串数组。例如,@RequestMapping("/user")表示处理以"/user"开头的URL请求。 2. method:指定请求的HTTP方法,可以是一个RequestMethod枚举值或枚举数组。例如,@RequestMapping(value="/user", method=RequestMethod.GET)表示处理GET方法的"/user"请求。 3. params:指定请求的参数条件,可以是一个字符串或字符串数组。例如,@RequestMapping(value="/user", params="id=1")表示只处理带有"id=1"参数的"/user"请求。 4. headers:指定请求的头部条件,可以是一个字符串或字符串数组。例如,@RequestMapping(value="/user", headers="Accept=application/json")表示只处理带有"Accept=application/json"头部的"/user"请求。 5. consumes:指定请求的Content-Type条件,可以是一个字符串或字符串数组。例如,@RequestMapping(value="/user", consumes="application/json")表示只接受Content-Type为"application/json"的"/user"请求。 6. produces:指定响应的Content-Type条件,可以是一个字符串或字符串数组。例如,@RequestMapping(value="/user", produces="application/json")表示只返回Content-Type为"application/json"的响应。 7. params和headers参数支持更复杂的表达式,如使用运算符(==、!=、>、<、>=、<=)和正则表达式等。 除了以上参数,@RequestMapping注解还支持其他一些参数,如path、name、defaultValue等,可以根据具体需求选择使用。 需要注意的是,Spring MVC还提供了一系列的派生注解,如@GetMapping、@PostMapping、@PutMapping、@DeleteMapping等,它们是对@RequestMapping的进一步封装,更加简洁易用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值