If we need to POST an object with validated field, we can send it from our FE (Angular, React, etc…) application with 2 main content types: JSON or “form encoded”.
Pay attenction to not use the @RequestBody annotation if you want to send those datas in the second one type.
The application automatically maps our request params into the request object param of our method.
@PostMapping(path = "/test", consumes = "application/json")
public String test(@RequestBody User user) { // Not works!
return user.toString();
}
@PostMapping(path = "/test", consumes = "application/x-www-form-urlencoded")
public String test(User user) {
return user.toString(); // the Object toString() method.
}