使用@pathvariable与@requestparam碰到的一些问题及解决
作者:feidao0 时间:2023-11-27 16:30:34
@pathvariable与@requestparam碰到的一些问题
一、@pathvariable
可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {x} 占位符可以通过@PathVariable("x") 绑定到操作方法的入参中。
@GetMapping("/test/{id}")
public String test(@PathVariable("id") String id){
System.out.println("test:"+id);
return SUCCESS;
}
可以看出使用@pathvariable注解它直接从url中取参,但是如果参数是中文就会出现乱码情况,这时应该使用@requestparam注解
二、@requestparam
它是直接从请求中取参,它是直接拼接在url后面(demo?name=张三)
@GetMapping("/demo")
public String test(@requestparam(value="name") String name){
System.out.println("test:"+name);
return SUCCESS;
}
注:如果参数不必须传入的话,我们从源码中可以看出两者required默认为true,如图:
所以我们可以这样写,只写一个例子
@GetMapping("/demo")
public String test(@requestparam(value="name", required = false) String name){
System.out.println("test:"+name);
return SUCCESS;
}
@PathVariable和@RequestParam的使用说明
要说明@PathVariable和@RequestParam的使用,首先介绍 @RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping:Annotation for mapping web requests onto methods in request-handling classes with flexible method signatures.Both Spring MVC and Spring WebFlux support this annotation.
RequestMapping注解有六个属性,常用的是value,method;还有consumes,produces,params,headers。
value属性:指定请求的实际地址,当只设置value属性时,默认省略不写
即:@RequestMapping("/hello")或@RequestMapping(value="/hello")
value的uri值为以下三类
A)可以指定为普通的具体值;
B)可以指定为含有某变量的值(URI Template Patterns with Path Variables);
C)可以指定为含正则表达式的值( URI Template Patterns with Regular Expressions)。
HelloController.java极简代码示例,既有PathVariable也有RequestParam
@RestController
public class HelloController {
@RequestMapping("/hellopv/{name}")
public String helloPV(@PathVariable String name, @RequestParam String username) {
String hello = "Hello " + username + " [" + name + "] !";
return hello;
}
}
感性认识一下,测试上述代码http://cos6743:8081/hellopv/tom?username=YangTom
@PathVariable是处理requet uri template中variable 的注解,实现了url入参绑定到方法参数上。
即:可以获取URL请求路径中的变量值,比如:RequestMapping("/hellopv/{name}")中的name
@RequestParam获取URL请求数据,是常用来处理简单类型的绑定注解。
通过Request.getParameter()获取入参,故此可以处理url中的参数,也可以处理表单提交的参数和上传的文件。
拓展
handler method 参数绑定常用的注解,根据处理的Request的不同内容分为四类常用类型
A、处理requet uri 部分(指uri template中variable)的注解: @PathVariable;
B、处理request header部分的注解: @RequestHeader, @CookieValue;
C、处理request body部分的注解:@RequestParam, @RequestBody;
D、处理attribute类型是注解: @SessionAttributes, @ModelAttribute;
来源:https://blog.csdn.net/feidao0/article/details/79493148
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 中TextView中跑马灯效果的实现方法
Android编程基于自定义View实现绚丽的圆形进度条功能示例
![](https://img.aspxhome.com/file/2023/7/84737_0s.png)
解决java 分割字符串成数组时,小圆点不能直接进行分割的问题
Maven的porfile与SpringBoot的profile结合使用案例详解
![](https://img.aspxhome.com/file/2023/2/58752_0s.png)
SpringBoot JPA实现增删改查、分页、排序、事务操作等功能示例
简单实现安卓里百度地图持续定位
idea创建SpringBoot项目时Type选maven project和maven pom有何区别
![](https://img.aspxhome.com/file/2023/9/57499_0s.png)
基于Java实现缓存Cache的深入分析
Java 六类运算符详解
Mybatis之typeAlias配置的3种方式小结
Java8中对于LocalDateTime的序列化和反序列化问题
解决springboot启动失败的问题('hibernate.dialect' not set)
基于SpringBoot核心原理(自动配置、事件驱动、Condition)
![](https://img.aspxhome.com/file/2023/4/58424_0s.jpg)
mybatis原理概述入门教程
java设计模式学习之工厂方法模式
![](https://img.aspxhome.com/file/2023/7/58607_0s.jpg)
Java NIO中四大核心组件的使用详解
IDEA中的.iml文件和.idea文件夹
![](https://img.aspxhome.com/file/2023/0/59680_0s.png)
深入探讨Linux静态库与动态库的详解(一看就懂)
Qt之调用C#的动态库的解决方法
![](https://img.aspxhome.com/file/2023/1/67061_0s.png)
Javassist之一秒理解java动态编程
![](https://img.aspxhome.com/file/2023/7/58897_0s.png)