一、注解纬度总结SpringBoot传参
1、pathVarible-路径传参
controller写法
@RestController
public class UserController {
@GetMapping("/user/{username}/{password}")
public String getUserInfo(@PathVariable("username") String username,
@PathVariable("password") String password){
return username+"\n"+password;
}
}
postMan测试
2、requestParam
@RestController
public class UserController {
@GetMapping("/user")
public String getUserInfo(@RequestParam("username") String username,
@RequestParam("password") String password){
return username+"\n"+password;
}
}
postman中如何进行测试
3、requestBody
@PostMapping("/addUsers")
public String addUsersList(@RequestBody List<Users> users){
return iuserService.addUsersList(users) > 0 ? "success" : "error";
}
postman中进行测试
4、HttpServletRequest
@GetMapping("/getHttpServletRequest")
public String httpServletRequestValueTest(HttpServletRequest request) {
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
return "---id:"+id+" ---name:"+name+" ---age:"+age;
}
二,传值类型纬度
1、get请求一般用pathVarible,requestParam,HttpServletRequest
2、post请求一般采用HttpServletRequest