SpringBoot实现前后端数据交互、json数据交互、Controller接收参数的几种常用方式...

1.获取参数的集中常见注解

  • @PathVariable:一般我们使用URI template样式映射使用,即url/{param}这种形式,也就是一般我们使用的GET,DELETE,PUT方法会使用到的,我们可以获取URL后所跟的参数。
  • @RequestParam:一般我们使用该注解来获取多个参数,在()内写入需要获取参数的参数名即可,一般在PUT,POST中比较常用。
  • @RequestBody:该注解和@RequestParam殊途同归,我们使用该注解将所有参数转换,在代码部分在一个个取出来,也是目前我使用到最多的注解来获取参数

2.获取请求路径参数

  1. get请求,url路径传参
    get请求一般通过url传参,如:

http://localhost:8080/piano/add?brand="xinde" & price = "1200"
后端要获取这些参数,可以使用@RequestParam注解


@RestController
public class HelloController {
    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(@RequestParam Integer id){
        return "id:"+id;
    }

  1. get请求,url路径参数

http://localhost:8080/piano/xinde/buyaoq/1200

后端可以使用@PathVariable接收路径参数

@RestController
public class HelloController {
    @RequestMapping(value="/piano/{brand}/{price}",method= RequestMethod.GET)
    public String sayHello(@PathVariable("price") Integer id,@PathVariable("name") String name){
        return "id:"+id+" name:"+name;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值