Spring @RequestBody Annotation Example
In this section we will learn about @RequestBody Annotation.
@RequestBody annotation is used to indicating a method parameter should be bind to the body of the HTTP request. Internally, this annotation uses HTTP Message converters to convert the body of HTTP requests to domain objects.
"HTTP message converters are used to convert HTTP request body (either JSON or XML) to Java objects and java objects back to XML or JSON for composing HTTP response"
{ "name" : "john", "email" : "john@gmail.in" }
Assume that we are sending this JSON in the request body, now inside the controller, we can bind this JSON data to a domain object.
@PostMapping("/users")
public void saveUser(@RequestBody User user) {
}
Now this will happen with the help of Jackson API which is present in the classpath. Spring would convert the incoming JSON to a User object from the request body (because we added the @RequestBody
annotation)
Note: RequestBody is of course not limited to JSON, It can handle multiple formats, including plain text and XML, but JSON is probably the most used format.
Spring @RequestBody example
Below is a @RequestBody
example to map the request body (usually JSON data) to the method parameter User
.
User.java
package com.knf.dev.demo.dto;
public class User {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserController.java
package com.knf.dev.demo.controller;
import com.knf.dev.demo.dto.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/")
public class UserController {
// @RequestBody maps web request's
// body to method parameter User Object
@PostMapping(path = "/users")
public User saveUser(@RequestBody User user) {
String name = user.getName();
String email = user.getEmail();
System.out.println("Name: "+ name);
System.out.println("Email: "+ email);
//userRepository.saveUser(user);
return user;
}
}
Try to send a POST JSON to the /user
endpoint using Postman.
Spring @RequestBody example – Map version
We also can use @RequestBody
to map the request body to the method parameter Map
.
UserController.java
package com.knf.dev.demo.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@PostMapping(path = "/login")
public Boolean validateLogin
(@RequestBody Map<String, String> login) {
if (login == null) return false;
String username = login.get("username");
String password = login.get("password");
// simple check
if ("knowledgefactory".equalsIgnoreCase(username)
&& "knf123#".equals(password)) {
return true;
} else {
return false;
}
}
}
Try to send a POST JSON to the /login
endpoint using Postman.