SpringBoot:url中获取参数的注解

一.注解

  • @ PathVariable
  • @RequestParam
  • @RequestBody

springboot中获取参数的注解主要为这三个。

二.@PathVariable

  • controller映射的路径上具有一个占位符,@PathVariable可以接收请求路径中占位符的值并将之赋值给所注解的参数。
	//  url:xxx/test/参数
    @GetMapping("/test/{name1}")
    public String test1(@PathVariable("name1") String name){
        return name;
    }

110

三.@RequestParam

每个参数都已Key=Value的形式跟在url的后方,@RequestParam可以获取url后面参数中与自己所定义的参数名的值并注入到所注解的参数中。

//  url:xxx?name=值&age=值
    @GetMapping("test")
    public String test2(@RequestParam("name")String name){
        return name;
    }

tom

四.@RequestBody

多用于content-type为application/json,@RequestBody会获取请求中所携带的json字符串内容并与注解的bean进行参数对比。

//  url:xxx  参数为json字符串  POST
    @PostMapping("test")
    public Person test3(@RequestBody Person person){
        return person ;
    }
  • 访问:http://127.0.0.1:8080/test , Content-Type为application/json,json字符串内容为{“name”:“tom”,“age”:“18”}
  • 测试工具为Postman,当然也可以用Swagger

{
“name”: “tom”,
“age”: “18”
}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值