Spring Boot 获取Post Body参数的方法
1. 通过 @RequestBody 注解获取 Post 参数
url格式:http://localhost/adduser
bean:
// 注意这里类的属性名要和post请求携带的参数名一样 public class UserModel { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
用这个bean来封装接收的参数:
@PostMapping("/addUser") public String addUser(@RequestBody UserModel user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "username is:"+username + " " + "password is:"+password; }
2. 通过 HttpServletRequest 接收参数
url格式:http://localhost/adduser
@PostMapping("/adduser") public String addUser(HttpServletRequest request) { String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println("username is:"+username); System.out.println("password is:"+password); return "username is:"+username + " " + "password is:"+password; }
4. 通过 bean 接收参数
url格式:http://localhost/adduser
bean:
// 注意这里类的属性名要和post请求携带的参数名一样 public class UserModel { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
用这个bean来封装接收的参数:
@PostMapping("/addUser") public String addUser(UserModel user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "username is:"+username + " " + "password is:"+password; }
1、作用@RequestParam:将请求参数绑定到控制器的方法参数。 2、语法@RequestParam(value=”参数名”, r ...