Spring boot @RequestBody annotation example

Hey guys in this post, we will discuss spring boot @RequestBody annotation and its usage.


@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.

{ "firstName" : "Elmer", "lastName" : "Fudd" }

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.

public void printData(@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.

Example on @RequestBody annotation

Let’s create a spring boot application and discuss the use of @RequestBody annotation

Create spring boot project

There different ways to create a spring boot application, you can follow the below articles to create a spring boot application

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add the maven dependencies

Open pom.xml and add the following dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot request body example</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container. It also adds the jackson-databind which is needed for serialization in HttpMessageConverter.

spring-boot-devtools dependency for automatic reloads or live reload of applications.

Create domain object

Now create a domain object User.java inside the in.bushansirgur.springboot.model package and add the following code

package in.bushansirgur.springboot.model;

public class User {
	private String firstName;
	private String lastName;
	private Integer age;

	public String getFirstName() {
		return firstName;

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public String getLastName() {
		return lastName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

	public Integer getAge() {
		return age;

	public void setAge(Integer age) {
		this.age = age;

	public String toString() {
		return "User [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";

Create controller

Now create a controller UserController.java and add the following code

package in.bushansirgur.springboot.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springboot.model.User;

public class UserController {
	public void printData(@RequestBody User user) {
		System.out.println("Printing the user data:"+user);

Run the application

You can run the application by executing the below command

mvn spring-boot:run

Let’s send the JSON data in the request body and see the result


we can see the result in the console

Printing the user data:User [firstName=Bushan, lastName=Sirgur, age=28]

About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *