How to Validate Request Body in Spring Boot

The spring boot rest supports for restful web services. The restful web service contains headers and body. The header contains the information about the body. The body contains the actual data. The data can be in any format. The restful web service normally transfers the data in JSON format.

The spring boot controller helps to convert the json data to java object and validates the data. Spring boot validates before executing the method in the controller. In this post, we will see, how to read request body in spring boot and how to validate request body in spring boot controller. The step by step procedure is added to validate the request body.

Step 1 – Add dependency in pom.xml

The “spring-boot-starter-validation” dependency will include all the jars needed for the validation of the request body. The web application needs the dependence of “spring-boot-starter-browser.” This will allow the tomcat server and start as a web application. The default “spring-boot-starter-test” test dependency will be applied to pom.xml for the unit testing.

If you have created a spring boot application, now you’re trying to incorporate the validation code, then add the “spring-boot-starter-validation” dependency to the pom.xml file. The complete pom.xml file will be as shown below.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring Boot Project</description>






Step 2 – Add @RequestBody annotation

The request body contains the actual data in json format. The annotation @RequestBody is used to handle the request body. This annotation converts the json string to the java object. It parses the json string and creates the java bean object and assigns the corresponding value in the object.

In this post, the json is converted to student object. Student object contains a variable “age”. Spring boot creates Student object and assigns value to the variable.

The example is shown in the next step along with validation tags.

Step 3 – Add @Valid annotation for validation

The @Valid annotation is used to specify the rest controller to be validated. The spring boot application validates the method before it is called.

package com.yawintutor;

import javax.validation.constraints.Min;

public class Student {

	private int age;

	public int getAge() {
		return age;

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


package com.yawintutor;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class TestController {

	ResponseEntity<String> student(@Valid @RequestBody Student student) {
		return ResponseEntity.ok("Your age is " + student.getAge());

Step 4 – Run the application

To validate the request body, start the application on the spring boot. Call the curl command or import and run in postman tool. 

curl -X POST \
  http://localhost:8080/student/ \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 6f258a7f-d361-44f8-9c9b-b5bdc2828250' \
  -d '{"age":"5"}'


Your age is 5

If the invalid json value is passed as part of url the validation will fail. The url below displays the curl command with invalid data.

curl -X POST \
  http://localhost:8080/student/ \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 6f258a7f-d361-44f8-9c9b-b5bdc2828250' \
  -d '{"age":"3"}'


    "timestamp": "2020-03-26T16:42:04.107+0000",
    "status": 400,
    "error": "Bad Request",
    "errors": [
            "codes": [
            "arguments": [
                    "codes": [
                    "arguments": null,
                    "defaultMessage": "age",
                    "code": "age"
            "defaultMessage": "must be greater than or equal to 5",
            "objectName": "student",
            "field": "age",
            "rejectedValue": 3,
            "bindingFailure": false,
            "code": "Min"
    "message": "Validation failed for object='student'. Error count: 1",
    "path": "/student/"

1 Comment

  • hari , December 17, 2020 @ 3:59 PM

    can we use @Valid like below ,
    can we use @Valid in normal method,if not possible is there any approach for this
    public student(@Valid Student student) {
    return ResponseEntity.ok(“Your age is ” + student.getAge());

Leave a Reply

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