SpringBoot Controller层接收参数的形式

1.参数存在于请求路径中

1.请求的参数:http://localhost:8080/postman/123

123作为参数传递到后台,接收方法:使用@PathVariable注解
@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
@RestController
@RequestMapping("postman")
public class controllerTest {
    /*
            localhost:8080/postman/***
     */
    @PostMapping("{id}")
    public void testPost(@PathVariable("id") Long id){
        System.out.println("接收到的参数"+id);
    }
}

2.请求的参数:http://localhost:8080/postman?id=123
接收方法:使用注解@RequestParam(“id”)

   /*
            localhost:8080/postman?id=1234
     */
    @GetMapping
    public void testPost2(@RequestParam("id") Long id){
        System.out.println("接收到的参数"+id);//接收到的参数1234
    }

2.参数在请求体中

1.参数以K-V键值对的形式发送

在这里插入图片描述2.后台接收

@PostMapping
public void testPost3(Person person){
   System.out.println("接收到的参数"+person);//接收到的参数Person(name=笑烂脸, age=23, sex=男)
}

2.参数以json对象的形式发送

注:前后端分离的项目,参数一般都是以JSON对象的形式发送
在这里插入图片描述
后台接收

@PostMapping
public void testPost4(@RequestBody Person person){前后端分离的项目,前端传递的数据都是json对象,所以后台想要接受对应的数据,必须要加@RequestBody注解,否则接收不了
    System.out.println("接收到的参数"+person);//接收到的参数Person(name=笑烂脸, age=23, sex=男)
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值