Springboot中参数传递的三个注解的使用
Springboot中参数传递的三个注解的使用
本文只要介绍springboot中有关参数传递的三个注解:@PathVariable 、@RequestParam 以及 @RequestBody的使用
@PathVariable
@PathVariable
注解主要是用来获取 url 参数,Spring Boot 支持 restfull 风格的 url,比如一个 GET
请求携带一个参数 id 过来,我们将 id 作为参数接收,可以使用 @PathVariable 注解
/**
* @Author Christy
* @DESC
* @Date 2020/11/12 9:56
**/
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/testPathVariable01/{id}/{name}")
public Map<String, Object> testPathVariable01(@PathVariable Integer id, @PathVariable(value = "name") String username){
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
jsonObject.put("username",username);
return ResultInfo.getDataMap(jsonObject);
}
}
如果想要url中占位符中的id值直接赋值到参数id中,需要保证url中的参数和方法接收参数一致,否则就无法接收。如果不一致的话,其实也可以解决,需要用@PathVariable 中的value
属性来指定对应关系。比如上面的参数name
。
我们用Postman来测试一下上面的代码
还有就是对于访问的url,占位符的位置可以在任何位置,不一定非要在最后,比如这样也行: /xxx/{id}/user 。另外,url 也支持多个占位符,方法参数使用同样数量的参数来接收,原理和一个参数是一样的
@GetMapping("/testPathVariable01/{id}/and/{name}")
public Map<String, Object> testPathVariable02(@PathVariable Integer id, @PathVariable(value = "name") String username){
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
jsonObject.put("username",username);
return ResultInfo.getDataMap(jsonObject);
}
同样的我们使用Postman测试一下上面的代码
@RequestParam
GET用法
@RequestParam
也是获取请求参数的,那么@RequestParam和@PathVariable有什么不同呢?
主要区别在于:
@PathValiable 是从 url 模板中获取参数值, 即这种风格的 url:http://localhost:8088/test/testPathVariable/{id} ;
@RequestParam 是从 request 里面获取参数值,即这种风格的 url: http://localhost:8088/test/testRequestparam?id=1
@GetMapping("/testRequestParam01")
public Map<String, Object> testRequestParam01(Integer id){
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
return ResultInfo.getDataMap(jsonObject);
}
@GetMapping("/testRequestParam02")
public Map<String, Object> testRequestParam02(@RequestParam Integer id){
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id);
return ResultInfo.getDataMap(jsonObject);
}
@GetMapping("/testRequestParam03")
public Map<String, Object> testRequestParam03(@RequestParam(value = "name") String username){
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", username);
return ResultInfo.getDataMap(jsonObject);
}
对上面的代码解释一下
- 页面传入的参数和类中接收的参数一致,@RequestParam注解可以省略,单个以及多个均适用;
- 界面传入的参数和类中接收的参数不一致,@RequestParam注解不可以省略,并且要在value属性中写明页面传入的参数名
适用Postman对上面三个方法进行测试如下
POST用法
@RequestParam注解还可以用于POST请求,接收前端表单提交的参数,假如前端通过表单提交username和
password两个参数,那我们可以使用@RequestParam来接收,用法和上面一样
@PostMapping("/testRequestParam04")
public Map<String, Object> testRequestParam04(@RequestParam String username, @RequestParam String password){
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", username);
jsonObject.put("password", password);
return ResultInfo.getDataMap(jsonObject);
}
适用Postman测试上面代码
但是通常表单提交的参数比较多,向上面那样一个一个的参数进行接收是不合理的,通常情况下我们都是封装成一个实体类传参,实体中的属性名和表单中的参数名一致即可;后台只需要接收这个实体类就行了,这种情况下就不需要加@RequestParam注解了
首先我们定义一个实体类
/**
* @Author Christy
* @DESC
* @Date 2020/11/12 10:50
**/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String username;
private String password;
}
其次编写我们的测试代码
@PostMapping("/testRequestParam05")
public Map<String, Object> testRequestParam05(User user){
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", user.getUsername());
jsonObject.put("password", user.getPassword());
return ResultInfo.getDataMap(jsonObject);
}
最后我们用Postman测试一下结果
@RequestBody
@RequestBody
注解用于接收前端传来的实体,接收参数也是对应的实体,比如前端通过 json 提交传
来两个参数 username 和password
@PostMapping("/testRequestBody01")
public Map<String, Object> testRequestBody01(@RequestBody User user) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", user.getUsername());
jsonObject.put("password", user.getPassword());
return ResultInfo.getDataMap(jsonObject);
}
我们用Postman测试一下结果
@RequestBody注解用于POST请求上,接收json实体参数。
@RequestBody和上面我们介绍的@RequestParam表单提交有点类似,只不过接收参数的格式不同,@RequestBody接收的是json实体,@RequestParam接收的是表单提交