@Valid плюс аннотации вроде @NotBlank, @Email и т.д.public class RegistrationRequest {
@Email(message = "Please provide a valid email address")
@NotBlank(message = "Email is required")
private String email;
@Size(min = 8, max = 16, message = "Password must be 8–16 characters long")
private String password;
@Pattern(
regexp = "^[0-9]{10,15}$",
message = "Phone number must be 10–15 digits"
)
private String phone;
// getters & setters
}By Default, if the validation fails Spring automatically returns a 400 Bad Request with a detailed error body like:
{
"timestamp": "2025-01-01T12:00:00Z",
"status": 400,
"errors": [
"Email is required",
"Age must be at least 18"
]
}You can customize the response by implementing a global exception handler
👉 Java Portal
