Tips: rest API POST content types


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.

Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.