Skip to main content

Posts

Showing posts with the label SpringBoot

Understanding @Validated in Spring Boot: A Complete Guide

In real-world applications, we often deal with user input—whether it’s coming from APIs, forms, or service calls. Validating this input is critical to ensure our application behaves correctly and securely. Spring Boot makes this easier with annotations like @Valid and @Validated . In this blog, we’ll dive deep into what @Validated is, how it works, and when to use it —with examples you can apply directly to your projects. 📌 What is @Validated ? @Validated is a Spring Framework annotation ( org.springframework.validation.annotation.Validated ) that enables bean validation (JSR-303/JSR-380, powered by Hibernate Validator by default). It tells Spring to: Validate incoming request data (DTOs). Validate method arguments in services. Enforce constraints like @NotNull , @Email , @Size , etc. 📝 Example 1: Using @Validated in a REST Controller Suppose you have a REST endpoint to create a user: @RestController @RequestMapping("/api/users") public class UserController...