@PathVariable 和 @RequestParam

(1)===================================================

转自:https://blog.csdn.net/weixin_45393094/article/details/108814901

、注解解释

@PathVariable 映射 URL 绑定的占位符
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过

@PathVariable(“xxx”) 绑定到操作方法的入参中。

一般与@RequestMapping(method = RequestMethod.GET)一起使用

@RequestMapping("/getUserById/{name}")
    public User getUser(@PathVariable("name") String name){
        return userService.selectUser(name);
    }

1、若方法参数名称和需要绑定的url中变量名称一致时,可以简写:

@RequestMapping("/getUser/{name}")
    public User getUser(@PathVariable String name){
        return userService.selectUser(name);
    }

2、若方法参数名称和需要绑定的url中变量名称不一致时,写成:

@RequestMapping("/getUserById/{name}")
    public User getUser(@PathVariable("name") String userName){
        return userService.selectUser(userName);
    }


二、代码实践


我们用postman来测试 /test/辰兮


我们controller层接到值然后打印出来 发现可以打印出

 
此时我们去掉@PathVariable发现再用postman测试发现值为空,即无法获取占位符中的参数



(2)===================================================

转载于:https://www.cnblogs.com/goloving/p/9241393.html

代码实例

  首先,上两个地址:

  地址1:http://localhost:8989/SSSP/emps?pageNo=2

  地址2:http://localhost:8989/SSSP/emp/7

  如果想获取地址1中的 pageNo的值 ‘2’ ,则使用  @RequestParam ,

  如果想获取地址2中的 emp/7 中的 ‘7 ’   则使用 @PathVariable

  实例如下:

复制代码
复制代码
  @RequestMapping(value="/getTaobao")
    public List<Taobao> doAddTaobao(@RequestParam(required = false) Integer status){
        return taobaoService.getTaobao(status);
    }
    @RequestMapping(value = "/auditTaobao/{id}")
    public void doAuditTaobao(@PathVariable Integer id){
        taobaoService.auditTaobao(id);
    }
复制代码
复制代码

  大道理不讲,原理也不分析就记忆一点,那一点呢? 看‘这个符号‘?’ 

  1、若获取的入参的 参数 是下面这种形式 就使用 @requestParam 去获取 参数‘2’

  /emps?pageNo=2

  2、若获取的入参的 参数 是下面这种形式 就使用 @PathVariable 去获取参数 ‘7’

  /emp/7

二、详细讲解

  RequestParam  汉语意思就是: 请求参数。顾名思义 就是获取参数的

  PathVariable  汉语意思是:路径变量。顾名思义,就是要获取一个url 地址中的一部分值,那一部分呢?

  RequestMapping 上说明了@RequestMapping(value="/emp/{id}"),我就是想获取你URL地址 /emp/ 的后面的那个 {id}的

  @PathVariable是用来获得请求url中的动态参数的

  因此,就看‘?’ 若是想获取 ‘?’ 后面的pageNo 的值 ‘2’, 就使用RequestParam 。若想获取的是url 地址的一部分 ‘7’ 就使用PathVariable

1、@pathVariable和RequestParam的区别:

  顾名思义,前者是从路径中获取变量也就是把路径当做变量,后者是从请求里面获取参数从请求来看:

  /Springmvc/user/page.do?pageSize=3&pageNow=2

  pageSize和pageNow应该是属于参数而不是路径,所以应该添加@RequestParam的注解。

  如果做成如下URL,则可以使用@PathVariable

  someUrl/{paramId},这里的paramId是路径中的变量,应使用@pathVariable

2、使用

@PathVariable

  当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId},这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

@RequestParam

(1)常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;

(2)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;

(3) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定

 

 

(3)===================================================

转自:https://blog.csdn.net/manonggeerdan/article/details/124125896

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)Get方式请求,参数放在url中时:

    不加@RequestParam注解:url可带参数也可不带参数,输入 localhost:8080/list1 以及 localhost:8080/list1?userId=xxx 方法都能执行
    加@RequestParam注解:url必须带有参数。也就是说你直接输入localhost:8080/list2 会报错,不会执行方法。只能输入localhost:8080/list2?userId=xxx 才能执行相应的方法

 

posted @   sensen~||^_^|||&  阅读(117)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示