Spring boot @RequestBody annotation example





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

Overview


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

@PostMapping("/users")
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.

Watch the video


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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>springbootrequestbody</artifactId>
	<version>v1</version>
	<name>springbootrequestbody</name>
	<description>Spring boot request body example</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

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;
	}

	@Override
	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;

@RestController
public class UserController {
	
	@PostMapping("/users")
	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

Screenshot-2021-02-14-at-11-28-35-PM

we can see the result in the console

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




Our Best Partners: https://nopeakasinot.com/ como retirar o dinheiro do jogo fortune tiger nopeakasinot.com alfcasino online kasino wildz casino nye casinoer danmark https://kasinopaynplay.com/ demo fishin frenzy fortune dragon bet

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply