作者简介

作者名:编程界明世隐

一、@RequestParam

针对QueryString参数传递,在请求URL中直接拼接请求参数,如URL?param1=value1& param2=value2

  1. 传递基本类型参数

在接收的方法中用@RequestParam注解修饰,指定前端传递的参数名称

@RequestMapping("QueryStringTest1")
public String QueryStringTest1(@RequestParam("id") String id,@RequestParam("name") String name){
    return "id is "+ id+",name is "+ name;
}

Postman 中调用

springboot controller 参数可以不传 springboot参数传递与接收_spring boot

如果传递的参数和接收的参数名一致,甚至可以省略相关配置

  • 省略1

@RequestParam(“id”) 中的参数名id被省略

@RequestMapping("QueryStringTest2")
public String QueryStringTest2(@RequestParam String id,@RequestParam String name){
    return "id is "+ id+",name is "+ name;
}

springboot controller 参数可以不传 springboot参数传递与接收_后端_02


注意:因为有RequestParam注解的存在,所以传参必须包含所有使用RequestParam注解的接收参数,否则会报错(允许传空,但是必须包含参数);

可以使用@RequestParam(value = “id”,required = false) 的方式,让id未传也可以。

springboot controller 参数可以不传 springboot参数传递与接收_传参_03

  • 省略2

@RequestParam 注解直接被省略

@RequestMapping("QueryStringTest3")
public String QueryStringTest3(String id, String name){
    return "id is "+ id+",name is "+ name;
}

springboot controller 参数可以不传 springboot参数传递与接收_后端_04


允许其中的参数不传,不会报错,但取值为null,比如name不传

springboot controller 参数可以不传 springboot参数传递与接收_传参_05

传参和入参名不一样的情况

@RequestMapping("QueryStringTest4")
public String QueryStringTest4(@RequestParam("id") String myId,@RequestParam("name") String myName){
   return "myId is "+ myId+",myName is "+ myName;
}

springboot controller 参数可以不传 springboot参数传递与接收_传参_06

  1. 传递对象类型参数

定义一个对象,属性名称和前端传递的参数名称一致即可

@RequestMapping("objectTest1")
 public String objectTest1(User user){
     return "user.id is "+ user.getId()+",user.name is "+ user.getName();
 }

springboot controller 参数可以不传 springboot参数传递与接收_java_07

未传的属性值为null

springboot controller 参数可以不传 springboot参数传递与接收_java_08

加上注解@RequestParam,会怎么样呢? 会报错,不要这样用

@RequestMapping("objectTest3")
public String objectTest3(@RequestParam User user){
    return "user.id is "+ user.getId()+",user.name is "+ user.getName();
}

springboot controller 参数可以不传 springboot参数传递与接收_java_09

  1. 传递数组、集合类型
  • 数组

拼接多个参数名称一样的参数即可,如URL?param=value1&param=value2&param=value3

@RequestMapping("arrayTest1")
public String[] arrayTest1(@RequestParam String[] name){
    for (String temp:name){
        System.out.println(temp);
    }
    return name;
}

springboot controller 参数可以不传 springboot参数传递与接收_后端_10

对同一个参数赋多个值,多个值之间用,隔开,如URL?param=value1,value2,value3

springboot controller 参数可以不传 springboot参数传递与接收_spring boot_11

  • 集合

只举例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;
}

springboot controller 参数可以不传 springboot参数传递与接收_传参_12

二、@PathVariable

路径传参方式是将参数直接包含在URL路径中,比如URL/paramValue1/paramValue2

在接口对应的请求路径中用{参数名}形式标出路径参数
在接口方法的参数上标注@PathVariable指名对应路径参数的参数名

  1. 传递基本类型参数
@RequestMapping("pathTest1/{id}/{name}")
 public String pathTest1(@PathVariable("id") String id, @PathVariable("name") String name){
     return "id is "+ id+",name is "+ name;
 }

springboot controller 参数可以不传 springboot参数传递与接收_RequestParam_13

能省略这个注解吗?我的结论是不可以,请看下面

@RequestMapping("pathTest2/{id}/{name}")
public String pathTest2(String id,String name){
    return "id is "+ id+",name is "+ name;
}

springboot controller 参数可以不传 springboot参数传递与接收_java_14

  1. 传递数组、集合类型
  • 数组
@RequestMapping("pathArrayTest1/{name}")
public String[] pathArrayTest1(@PathVariable("name") String[] names){
    for (String name:names){
        System.out.println("array name:"+name);
    }
    return names;
}

springboot controller 参数可以不传 springboot参数传递与接收_java_15

  • 集合
@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;
}

springboot controller 参数可以不传 springboot参数传递与接收_传参_16

  • 文件
// MultipartFile接收
@PostMapping("upload")
public String uploadFile(@RequestParam("file") MultipartFile myFile){
    return "type:"+myFile.getContentType()
            +" fileName:"+myFile.getOriginalFilename()
            +" size:"+myFile.getSize();
}

测试的注意点

  1. 用post
  2. 用Body的form-data
  3. 参数类型选择File 而不是默认的Text

springboot controller 参数可以不传 springboot参数传递与接收_spring boot_17

三、@RequestBody

针对body表单传参,RequestBody只能有一个

  1. 传递基本类型参数
@RequestMapping("bodyTest1")
public String bodyTest1(@RequestBody String name){
   return "name is:"+name;
}

springboot controller 参数可以不传 springboot参数传递与接收_java_18


2. 传递对象类型参数

比如User 对象

@RequestMapping("bodyTest2")
public String bodyTest2(@RequestBody User user){
    return "name is:"+user.getName() + " age is:"+user.getAge();
}

springboot controller 参数可以不传 springboot参数传递与接收_java_19


3. 传递数组、集合类型

  • 数组
@RequestMapping("bodyTest3")
public String bodyTest3(@RequestBody String[] users){
    return Arrays.asList(users).toString();
}

springboot controller 参数可以不传 springboot参数传递与接收_spring boot_20

  • 集合

User对象

@RequestMapping("bodyTest4")
public String bodyTest4(@RequestBody List<User> users){
    return users.toString();
}

springboot controller 参数可以不传 springboot参数传递与接收_spring boot_21

String类型

@RequestMapping("bodyTest5")
public String bodyTest5(@RequestBody List<String> users){
    return users.toString();
}

springboot controller 参数可以不传 springboot参数传递与接收_RequestParam_22


*Json对象

添加依赖

<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");
}

springboot controller 参数可以不传 springboot参数传递与接收_java_23

四、HttpServletRequest

明哥以前经常用

@RequestMapping("httpRequest")
public String httpRequest(HttpServletRequest request){
    return "name is :"+request.getParameter("name")+
            " age is :"+request.getParameter("age");
}

springboot controller 参数可以不传 springboot参数传递与接收_RequestParam_24


或者

springboot controller 参数可以不传 springboot参数传递与接收_java_25

五、@PathVariable+@RequestParam混合

@RequestMapping("pathAndParam/{id}")
public String pathAndParam(@PathVariable("id") String id, @RequestParam("name") String name){
    return "id is "+ id+",name is "+ name;
}

springboot controller 参数可以不传 springboot参数传递与接收_spring boot_26

六、@PathVariable+@RequestBody混合

@RequestMapping("pathAndBody/{id}")
public String pathAndBody(@PathVariable("id") String id, @RequestBody User user){
    return "id is "+ id+",name is "+ user.getName();
}

springboot controller 参数可以不传 springboot参数传递与接收_spring boot_27

七、无注解传参

@RequestMapping("other")
public String other(User user){
    return "name is:"+user.getName() + " age is:"+user.getAge();
}

springboot controller 参数可以不传 springboot参数传递与接收_后端_28