@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来接受参数

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值