How to Use @Email Annotation in Spring Boot for Validation
In Spring Boot, use the
@Email annotation on a String field to validate that it contains a valid email address format. Combine it with @Valid in your controller or service to trigger validation automatically.Syntax
The @Email annotation is placed on a String field to check if the value is a valid email format. It is part of the javax.validation.constraints package. You can customize the error message with the message attribute.
Example parts:
@Email: Validates email format.message: Custom error message shown if validation fails.@NotBlank: Often combined to ensure the field is not empty.
java
import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; public class User { @NotBlank(message = "Email is required") @Email(message = "Invalid email format") private String email; // getters and setters }
Example
This example shows a Spring Boot REST controller that accepts a user object with an email field. The @Email annotation validates the email format automatically when the request is received. If the email is invalid, Spring Boot returns a 400 Bad Request with the validation error.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; @SpringBootApplication public class EmailValidationApp { public static void main(String[] args) { SpringApplication.run(EmailValidationApp.class, args); } } @RestController @Validated class UserController { @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User email is valid: " + user.getEmail()); } } class User { @NotBlank(message = "Email is required") @Email(message = "Invalid email format") private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Output
POST /users with body {"email":"test@example.com"} returns 200 OK with message "User email is valid: test@example.com"
POST /users with body {"email":"invalid-email"} returns 400 Bad Request with validation error "Invalid email format"
Common Pitfalls
- Not using
@Validon the controller method parameter will skip validation. - Using
@Emailalone without@NotBlankallows empty strings, which may not be desired. - Custom error messages must be set on
@Emailto provide clear feedback. - Validation errors must be handled or Spring Boot will return default error responses.
java
/* Wrong: Missing @Valid, validation won't trigger */ @PostMapping("/users") public ResponseEntity<String> createUser(@RequestBody User user) { return ResponseEntity.ok("User email is valid: " + user.getEmail()); } /* Right: Add @Valid to trigger validation */ @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User email is valid: " + user.getEmail()); }
Quick Reference
Summary tips for using @Email in Spring Boot:
- Place
@Emailon String fields to validate email format. - Combine with
@NotBlankto prevent empty values. - Use
@Validon controller method parameters to enable validation. - Customize error messages with the
messageattribute. - Handle validation errors gracefully in your application.
Key Takeaways
Use @Email on String fields to validate email format in Spring Boot.
Always add @Valid on controller parameters to trigger validation.
Combine @Email with @NotBlank to avoid empty email values.
Customize error messages with the message attribute for clarity.
Handle validation errors to provide user-friendly feedback.