SpringBoot传参
常见GET请求和POST请求的区别
1.get请求无消息体,只能携带少量数据,且不安全
post请求有消息体,可以携带大量数据,且安全
2.携带数据的方式:
get请求将数据放在url地址中
post请求将数据放在消息体body中
传参区别
-
@GetMapping只能通过url传参数。所对应的接口参数只能是用@RequestParam注解或者不注解(即只能通过参数传参或者路径传参)
-
@PostMapping既可以通过url传参数,也可以通过body传json参数。所对应的接口参数可以有@RequestParam注解,也可以有@RequestBody注解,也可以没有注解。(多了json的情况 大部分用json)
-
不管是@GetMapping还是@PostMapping,除了@RequestBody注解对应的参数是通过json在body里面传参数外,@RequestParam注解和没有注解都是在url中传参数.
-
用@RequestParam注解修饰的字段前端必须有对应的参数传过来,用@RequestBody修饰的类,前端至少要传一个空的json串,json串内容不一定需要和类对应,json串中只要有类的字段,后端的类就会从中取出对应的字段并赋值
-
对于类对象的参数,不管对于@RequestBody还是@RequestParam还是没有注解,前端不管传来多少个字段,后端的类对象只取类中包含的对象。
@PathVariable(路径传参)
@GetMapping("/getId/{id}")
public String pathVariableTest(@PathVariable Integer id) {
return "id: "+id;
}
@GetMapping("/getId/{id}")
public String pathVariableTest(@PathVariable("id") Integer id) {
return "id: "+id;
}
@GetMapping("/getId/{idValue}")
public String pathVariableTest(@PathVariable("idValue") Integer id) {
return "id: "+id;
}
虽然说三种方式都是成功的,但是推荐使用第二种,也就是明确地指定对应参数对应名称。第一种方式不注明参数名称的,仅仅在单个参的时候选择使用。
最后给出推荐使用的第二种方式的传接多参例子:
@GetMapping("/getIdAny/{id}/{name}")
public String pathVariableTestAny(@PathVariable("id") Integer id,@PathVariable("name") String name) {
return "**id: "+id+" **name: "+name;
}
@RequestParam(参数传参)
什么注解都没有就是参数传参
单个参数传参
//只有一个参数 并且参数值和传的参数值相同
@GetMapping("/getId")
public String requestParamTest( Integer id) {
return "id: "+id;
}
//可以不加注解@RequestParam
@GetMapping("/getId")
public String requestParamTest( @RequestParam Integer id) {
return "id: "+id;
}
//给传过来的参数命名为id
@GetMapping("/getId")
public String requestParamTest( @RequestParam("id") Integer id) {
return "id: "+id;
}
@GetMapping("/getId")
public String requestParamTest( @RequestParam("idValue") Integer id) {
return "id: "+id;
}
第四种,可以看到在@RequestParam里面给参数id取名为: idValue,这时候传参时参数名称也需要写成idValue即可。
多个参数传参
以实体类传参接参方式
@GetMapping("/getValue")
public String entityValueTest( User user) {
return user.toString();
}
分开分别接参
@GetMapping("/getIdAny")
public String requestParamTestAny( @RequestParam("id") Integer id,@RequestParam("name") String name,@RequestParam Integer age) {
return "**id: "+id+" **name: "+name+" **age: "+age;
}
上传文件
针对使用@RequestParam,补充一下上传文件的是需要注意的以及方式:
简单示例一下怎么接收以及传递文件(使用MultipartFile接收):
@PostMapping("upload")
public String upload(@RequestParam("myFile") MultipartFile file) {
return "---file name:" + file.getOriginalFilename()+
"---file type:" + file.getContentType()+
"---file size:" + file.getSize();
}
@RequestBody
以json格式传递,接收方式较多(**一定要记住Content-Type为application/json)
Get 方式 和 Post方式 其实都是可以有requestBody的,只不过一般来说post用的较多
通过实体类接参
@GetMapping("/getRequestBodyValue")
public String RequestBodyTest(@RequestBody User user) {
return user.toString();
}
通过Map接参
@PostMapping("/getRequestBodyValue")
public String RequestBodyTest(@RequestBody Map userMap) {
return userMap.toString();
}
通过JsonObject接参
<!--添加fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
@PostMapping("/getRequestBodyValue")
public String RequestBodyTest(@RequestBody JSONObject jsonObject) {
Integer id = jsonObject.getInteger("id");
String name = jsonObject.getString("name");
Integer age = jsonObject.getInteger("age");
return "---id:"+id+" ---name:"+name+" ---age:"+age;
}
通过String接参
先使用String把body里面的数据获取出来,再转换成jsonobject再进行参数解析
@PostMapping("/getRequestBodyValue")
public String RequestBodyTest(@RequestBody String jsonStr) {
JSONObject jsonObject= JSON.parseObject(jsonStr); //将jsonStr转化为JSONObject,再进行取值
Integer id = jsonObject.getInteger("id");
String name = jsonObject.getString("name");
Integer age = jsonObject.getInteger("age");
return "---id:"+id+" ---name:"+name+" ---age:"+age;
}
@RequestHeader
取出放在header里面的值
@ResponseBody
@RequestMapping("/getMyHeaderParam")
public String getOrderList(@RequestHeader String token,@RequestHeader String uuid) {
return "----token:"+token+"--- uuid:"+uuid;
}
传递参数与接收参数不对应的理解
传递参数和接收的实体不对应或者说是为空时的情况 接下来是比较好的理解
eg: 后端有一个类User的对象
未用@RequestBody修饰,只能通过url传参:
传的参数和类对应
传的参数为空
传的参数比需要接收实体的属性少
传的参数比需要接收实体的属性多
使用@RequestBody修饰,只能通过body的json字符串传参
当然url里面也可以传值,只是不会被@RequestBody接收,只能被@RequestParam和无注解的接收
传的参数与类对应
传的参数比需要接收实体的属性少
传的参数比需要接收实体的属性多
总结:
后端只要求前端必须传被注解的变量,未被注解的变量前端可传可不传,前端传了后端就会取,不传后端默认该变量为null
前端传的参数不一定与类变量一一对应也是不会报错的,但最好还是一一对应
@RequestParam(“easd”)指定了前端必须传的参数名,而@RequestBody只指定前端必须传一个json在body里面,至于json字符串的内容,不一定与类变量一一对应