作者名:编程界明世隐
简介:CSDN博客专家,从事软件开发多年,精通Java、JavaScript,博主也是从零开始一步步把学习成长、深知学习和积累的重要性,喜欢跟广大ADC一起打野升级,欢迎您关注,期待与您一起学习、成长、起飞!明哥QQ:283582761
测试工具 IDEA、postman
针对QueryString参数传递,在请求URL中直接拼接请求参数,如URL?param1=value1¶m2=value2
在接收的方法中用@RequestParam注解修饰,指定前端传递的参数名称
@RequestMapping("QueryStringTest1")
public String QueryStringTest1(@RequestParam("id") String id,@RequestParam("name") String name){
return "id is "+ id+",name is "+ name;
}
如果传递的参数和接收的参数名一致,甚至可以省略相关配置
@RequestParam(“id”) 中的参数名id被省略
@RequestMapping("QueryStringTest2")
public String QueryStringTest2(@RequestParam String id,@RequestParam String name){
return "id is "+ id+",name is "+ name;
}
注意:因为有RequestParam注解的存在,所以传参必须包含所有使用RequestParam注解的接收参数,否则会报错(允许传空,但是必须包含参数);
可以使用@RequestParam(value = “id”,required = false) 的方式,让id未传也可以。
@RequestParam 注解直接被省略
@RequestMapping("QueryStringTest3")
public String QueryStringTest3(String id, String name){
return "id is "+ id+",name is "+ name;
}
允许其中的参数不传,不会报错,但取值为null,比如name不传
传参和入参名不一样的情况
@RequestMapping("QueryStringTest4")
public String QueryStringTest4(@RequestParam("id") String myId,@RequestParam("name") String myName){
return "myId is "+ myId+",myName is "+ myName;
}
定义一个对象,属性名称和前端传递的参数名称一致即可
@RequestMapping("objectTest1")
public String objectTest1(User user){
return "user.id is "+ user.getId()+",user.name is "+ user.getName();
}
未传的属性值为null
加上注解@RequestParam,会怎么样呢? 会报错,不要这样用
@RequestMapping("objectTest3")
public String objectTest3(@RequestParam User user){
return "user.id is "+ user.getId()+",user.name is "+ user.getName();
}
拼接多个参数名称一样的参数即可,如URL?param=value1¶m=value2¶m=value3
@RequestMapping("arrayTest1")
public String[] arrayTest1(@RequestParam String[] name){
for (String temp:name){
System.out.println(temp);
}
return name;
}
对同一个参数赋多个值,多个值之间用,隔开,如URL?param=value1,value2,value3
只举例List,其他类似
@RequestMapping("listTest1")
public List listTest1(@RequestParam List name){
for(int i=0;i<name.size();i++){
System.out.println("list name==="+name.get(i));
}
return name;
}
路径传参方式是将参数直接包含在URL路径中,比如URL/paramValue1/paramValue2
在接口对应的请求路径中用{参数名}形式标出路径参数
在接口方法的参数上标注@PathVariable指名对应路径参数的参数名
@RequestMapping("pathTest1/{id}/{name}")
public String pathTest1(@PathVariable("id") String id, @PathVariable("name") String name){
return "id is "+ id+",name is "+ name;
}
能省略这个注解吗?我的结论是不可以,请看下面
@RequestMapping("pathTest2/{id}/{name}")
public String pathTest2(String id,String name){
return "id is "+ id+",name is "+ name;
}
@RequestMapping("pathArrayTest1/{name}")
public String[] pathArrayTest1(@PathVariable("name") String[] names){
for (String name:names){
System.out.println("array name:"+name);
}
return names;
}
@RequestMapping("pathListTest1/{name}")
public List pathListTest1(@PathVariable("name") List names) {
for (int i=0;i<names.size();i++) {
System.out.println("list name:" + names.get(i));
}
return names;
}
// MultipartFile接收
@PostMapping("upload")
public String uploadFile(@RequestParam("file") MultipartFile myFile){
return "type:"+myFile.getContentType()
+" fileName:"+myFile.getOriginalFilename()
+" size:"+myFile.getSize();
}
测试的注意点
- 用post
- 用Body的form-data
- 参数类型选择File 而不是默认的Text
针对body表单传参,RequestBody只能有一个
@RequestMapping("bodyTest1")
public String bodyTest1(@RequestBody String name){
return "name is:"+name;
}
比如User 对象
@RequestMapping("bodyTest2")
public String bodyTest2(@RequestBody User user){
return "name is:"+user.getName() + " age is:"+user.getAge();
}
@RequestMapping("bodyTest3")
public String bodyTest3(@RequestBody String[] users){
return Arrays.asList(users).toString();
}
User对象
@RequestMapping("bodyTest4")
public String bodyTest4(@RequestBody List<User> users){
return users.toString();
}
String类型
@RequestMapping("bodyTest5")
public String bodyTest5(@RequestBody List<String> users){
return users.toString();
}
添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
后端代码
@RequestMapping("bodyTest6")
public String bodyTest6(@RequestBody JSONObject user){
return "name is :"+user.get("name")+
" age is :"+user.get("age");
}
明哥以前经常用
@RequestMapping("httpRequest")
public String httpRequest(HttpServletRequest request){
return "name is :"+request.getParameter("name")+
" age is :"+request.getParameter("age");
}
@RequestMapping("pathAndParam/{id}")
public String pathAndParam(@PathVariable("id") String id, @RequestParam("name") String name){
return "id is "+ id+",name is "+ name;
}
@RequestMapping("pathAndBody/{id}")
public String pathAndBody(@PathVariable("id") String id, @RequestBody User user){
return "id is "+ id+",name is "+ user.getName();
}
@RequestMapping("other")
public String other(User user){
return "name is:"+user.getName() + " age is:"+user.getAge();
}
这节总结了“ Springboot传参详解 ”,希望能对大家有所帮助,请各位小伙伴帮忙 【点赞】+【收藏】, 如果有兴趣跟小明哥一起学习Java和前端的,【关注一波】不迷路哦。
请到文章下方帮忙【一键三连】谢谢哈!
【1】Java小游戏(俄罗斯方块、飞机大战、植物大战僵尸等)
【2】JavaWeb项目实战(图书管理、在线考试、宿舍管理等)
【3】JavaScript精彩实例(飞机大战、贪吃蛇、验证码等)
【4】Java小白入门200例
【5】从零学Java、趣学Java
【6】Idea从零到精通