@RequestParam使用

本文探讨了@RequestParam在前后端交互中的作用,包括加与不加的区别:前端参数名需与后端变量名匹配,@RequestParam指定参数必传或非必传,以及指定参数名和默认值。此外,当接口同时被前端和后端RPC调用时,使用@RequestParam的重要性。总结了加不加注解在URL访问时的不同效果。
摘要由CSDN通过智能技术生成

1、加与不加的区别

@RequestMapping("/list1")
public String test1(int userId) {
  return "list";
}
@RequestMapping("/list2")
public String test2(@RequestParam int userId) {
  return "list";
}

(1)不加@RequestParam前端的参数名需要和后端控制器的变量名保持一致才能生效

(2)不加@RequestParam参数为非必传,加@RequestParam写法参数为必传。但@RequestParam可以通过@RequestParam(required = false)设置为非必传。

(3)@RequestParam可以通过@RequestParam(“userId”)或者@RequestParam(value = “userId”)指定传入的参数名。

(4)@RequestParam可以通过@RequestParam(defaultValue = “0”)指定参数默认值

(5)如果接口除了前端调用还有后端RPC调用,则不能省略@RequestParam,否则RPC会找不到参数报错

(6)访问时:

  • 不加@RequestParam注解:url可带参数也可不带参数,输入 localhost:8080/list1 以及 localhost:8080/list
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值