0
0
Spring Bootframework~5 mins

DTO validation in Spring Boot

Choose your learning style9 modes available
Introduction

DTO validation helps check if the data sent to your app is correct before using it. It stops bad or missing data early.

When receiving user input from a web form to create or update data.
When accepting JSON data in a REST API request.
When you want to ensure required fields are filled and follow rules like email format or number ranges.
When you want to give clear error messages if data is wrong.
When you want to keep your app safe from invalid or harmful data.
Syntax
Spring Boot
public class UserDTO {
    @NotNull
    @Size(min = 2, max = 30)
    private String name;

    @Email
    private String email;

    @Min(18)
    private Integer age;

    // getters and setters
}

Use annotations like @NotNull, @Size, @Email, @Min on DTO fields.

Spring Boot automatically checks these when you add @Valid in your controller method parameter.

Examples
This example checks that productName is not empty and price is a positive number.
Spring Boot
public class ProductDTO {
    @NotBlank
    private String productName;

    @Positive
    private Double price;

    // getters and setters
}
This example ensures username and password are not empty strings.
Spring Boot
public class LoginDTO {
    @NotEmpty
    private String username;

    @NotEmpty
    private String password;

    // getters and setters
}
Sample Program

This Spring Boot controller accepts a POST request with JSON data for a user. It checks the data using DTO validation annotations. If data is invalid, Spring returns errors automatically. If valid, it returns a success message.

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

@RestController
@RequestMapping("/users")
@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 must be valid")
        private String email;

        @Min(value = 18, message = "Age must be at least 18")
        private Integer age;

        // getters and setters
        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; }
        public Integer getAge() { return age; }
        public void setAge(Integer age) { this.age = age; }
    }

    @PostMapping
    public ResponseEntity<String> createUser(@RequestBody @Valid UserDTO user) {
        return ResponseEntity.ok("User " + user.getName() + " created successfully");
    }
}
OutputSuccess
Important Notes

Always add @Valid before the DTO parameter in controller methods to trigger validation.

Validation errors automatically return HTTP 400 with messages explaining what is wrong.

You can customize error messages using the message attribute in annotations.

Summary

DTO validation checks input data early to keep your app safe and clean.

Use annotations on DTO fields and @Valid in controllers to enable validation.

Spring Boot handles errors and responses for invalid data automatically.