0
0
Spring Bootframework~5 mins

Request validation preview in Spring Boot

Choose your learning style9 modes available
Introduction

Request validation preview helps check if the data sent to your app is correct before processing it. This stops errors and keeps your app safe.

When users submit forms with required fields like name or email.
When APIs receive data from other apps and need to ensure it is valid.
When you want to give users quick feedback about wrong or missing input.
When you want to avoid saving bad data to your database.
When you want to keep your app secure from invalid or harmful data.
Syntax
Spring Boot
public ResponseEntity<?> methodName(@RequestBody @Valid YourDto dto) {
    // method code
}
Use @Valid before the parameter to trigger validation.
Add validation annotations like @NotNull, @Size inside your DTO class.
Examples
This DTO class uses validation annotations to require a name and a valid email.
Spring Boot
public class UserDto {
    @NotNull(message = "Name is required")
    private String name;

    @Email(message = "Email should be valid")
    private String email;

    // getters and setters
}
This controller method validates the incoming user data before processing.
Spring Boot
@PostMapping("/users")
public ResponseEntity<String> addUser(@RequestBody @Valid UserDto user) {
    return ResponseEntity.ok("User is valid");
}
Sample Program

This Spring Boot controller defines a UserDto with validation rules. The addUser method checks the data sent in the request body. If the data is invalid, Spring Boot automatically returns an error response. If valid, it returns a success message.

Spring Boot
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@Validated
public class UserController {

    public static class UserDto {
        @NotNull(message = "Name is required")
        @Size(min = 2, max = 30, message = "Name must be 2-30 characters")
        private String name;

        @Email(message = "Email should be valid")
        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; }
    }

    @PostMapping("/users")
    public ResponseEntity<String> addUser(@RequestBody @Valid UserDto user) {
        return ResponseEntity.ok("User is valid: " + user.getName());
    }
}
OutputSuccess
Important Notes

Spring Boot automatically returns 400 Bad Request if validation fails.

Customize error messages in your DTO annotations for clearer feedback.

Use @Validated on the controller class to enable validation support.

Summary

Request validation checks input data before using it.

Use @Valid and validation annotations in DTO classes.

Spring Boot handles errors automatically and returns helpful messages.