How to Use @Valid Annotation in Spring Boot for Validation
In Spring Boot, use the
@Valid annotation on method parameters like request bodies or form objects to trigger automatic validation based on constraints. Combine it with @RequestBody and constraint annotations (e.g., @NotNull) on your model fields to validate input data before processing.Syntax
The @Valid annotation is placed on a method parameter in a controller to activate validation. It works with Java Bean Validation annotations on the object's fields.
@Valid: Triggers validation on the annotated parameter.@RequestBody: Binds HTTP request JSON to a Java object.- Constraint annotations (e.g.,
@NotNull,@Size): Define validation rules on fields.
java
public ResponseEntity<String> createUser(@Valid @RequestBody User user) { // method body }
Example
This example shows a Spring Boot REST controller that validates a User object sent in the request body. If validation fails, Spring Boot returns a 400 Bad Request with error details.
java
import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Email; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { public static class User { @NotBlank(message = "Name must not be blank") private String name; @Email(message = "Email should be valid") private String email; // 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; } } @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User is valid: " + user.getName()); } }
Output
HTTP 200 OK with body: "User is valid: [name]" if valid
HTTP 400 Bad Request with validation error details if invalid
Common Pitfalls
- Forgetting to add
@Validmeans validation won't run automatically. - Not using
@RequestBodyor wrong parameter type can cause binding errors. - Missing constraint annotations on fields means no validation rules.
- Not handling validation errors can lead to unclear responses; Spring Boot handles this by default but can be customized.
java
/* Wrong: Missing @Valid - validation is skipped */ @PostMapping("/users") public ResponseEntity<String> createUser(@RequestBody User user) { return ResponseEntity.ok("User is valid: " + user.getName()); } /* Right: Add @Valid to trigger validation */ @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User is valid: " + user.getName()); }
Quick Reference
Use this quick guide to remember how to apply @Valid in Spring Boot:
| Step | Description |
|---|---|
| 1 | Add @Valid before the method parameter to validate. |
| 2 | Use @RequestBody to bind JSON to the object. |
| 3 | Annotate model fields with validation constraints like @NotNull, @Size, @Email. |
| 4 | Handle validation errors automatically or customize error responses. |
| 5 | Ensure jakarta.validation dependency is included in your project. |
Key Takeaways
Add
@Valid on controller method parameters to enable automatic validation.Use constraint annotations on model fields to define validation rules.
Combine
@Valid with @RequestBody for JSON input validation.Spring Boot returns 400 Bad Request automatically if validation fails.
Always include the Jakarta Bean Validation dependency in your project.