Main Tutorials

Spring @RequestBody Annotation

In the Spring framework, the @RequestBody annotation maps the web request’s body to the method’s parameter, usually a domain object or a Map.

Table of contents

1. Spring @RequestBody example

Below is a @RequestBody example to map the request body (usually JSON data) to the method parameter LoginObject.

LoginObject.java

package com.mkyong;

public class LoginObject {
  private String username;
  private String password;

  public LoginObject(String username, String password) {
      this.username = username;
      this.password = password;
  }

  //getters setters...
}
HelloController.java

package com.mkyong;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  // @RequestBody maps web request's body to method parameter LoginObject
  @RequestMapping(path = "/login", method = RequestMethod.POST)
  public boolean validateLogin(@RequestBody LoginObject login) {

      if (login == null) return false;
      String username = login.getUsername();
      String password = login.getPassword();

      // simple check
      if ("mkyong".equalsIgnoreCase(username) && "123456".equals(password)) {
          return true;
      } else {
          return false;
      }
  }

}

Try to send a POST JSON to the /login endpoint.

Correct username and password.

Terminal

curl -i -H "Content-Type: application/json" \
  -d '{"username":"mkyong","password":"123456"}' \
  -X POST "http://localhost:8080/login"

HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 23 Aug 2022 09:33:41 GMT

true

Wrong password.

Terminal

curl -i -H "Content-Type: application/json" \
  -d '{"username":"mkyong"}' \
  -X POST "http://localhost:8080/login"

HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 23 Aug 2022 09:34:35 GMT

false

2. Spring @RequestBody example – Map version

We also can use @RequestBody to map the request body to the method parameter Map.

HelloController.java

package com.mkyong;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class HelloController {

  @RequestMapping(path = "/v2/login", method = RequestMethod.POST)
  public boolean validateLoginMapVersion(@RequestBody Map<String, String> login) {

      if (login == null) return false;
      String username = login.get("username");
      String password = login.get("password");

      // simple check
      if ("mkyong".equalsIgnoreCase(username) && "123456".equals(password)) {
          return true;
      } else {
          return false;
      }
  }

}

Try to send a POST JSON to the /v2/login endpoint.

Correct username and password.

Terminal

curl -i -H "Content-Type: application/json" \
  -d '{"username":"mkyong","password":"123456"}' \
  -X POST "http://localhost:8080/v2/login"
  
HTTP/1.1 200

Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 23 Aug 2022 09:38:46 GMT

true

Wrong password.

Terminal

curl -i -H "Content-Type: application/json" \
  -d '{"username":"mkyong","password":"888"}' \
  -X POST "http://localhost:8080/v2/login"   

HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 23 Aug 2022 09:38:56 GMT

false

3. Download Source Code

4. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments