← All tutorials

GoCareerGo Tutorials

Spring Boot Tutorial

Auto-configuration, REST APIs, JPA, AOP, caching, microservices and Spring Cloud.

Bean Validation (@Valid)

Declarative validation on request DTOs using JSR-380 annotations — invalid requests are rejected before your business logic runs.

public class CreateUserRequest {
    @NotBlank private String name;
    @Email private String email;
    @Min(18) private int age;
}

@PostMapping
public User create(@Valid @RequestBody CreateUserRequest req) {
    // Spring returns 400 automatically if validation fails
}
  • @NotNull, @NotBlank, @Size, @Min/@Max, @Email, @Pattern are the common annotations
  • Requires spring-boot-starter-validation on the classpath
  • Combine with @ControllerAdvice to customize the 400 error body