0
0
Spring Bootframework~30 mins

@Email and @Pattern in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Validating User Email and Username with @Email and @Pattern in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to register users. You want to make sure the email addresses are valid and usernames follow specific rules.
🎯 Goal: Create a User class with fields email and username. Use @Email to validate the email format and @Pattern to ensure the username contains only letters and numbers, with length between 3 and 10 characters.
📋 What You'll Learn
Create a User class with email and username fields
Use @Email annotation on the email field
Use @Pattern annotation on the username field with regex ^[a-zA-Z0-9]{3,10}$
Include getter and setter methods for both fields
💡 Why This Matters
🌍 Real World
Validating user input like email and username is common in web applications to ensure data quality and security.
💼 Career
Understanding validation annotations in Spring Boot is essential for backend developers working on user registration and data validation.
Progress0 / 4 steps
1
Create the User class with email and username fields
Create a public class called User with two private String fields: email and username.
Spring Boot
Need a hint?

Define the class and declare the two fields exactly as email and username.

2
Add @Email annotation to the email field
Add the @Email annotation above the email field in the User class. Also import jakarta.validation.constraints.Email.
Spring Boot
Need a hint?

Place @Email directly above the email field.

3
Add @Pattern annotation to the username field
Add the @Pattern annotation above the username field with the regex "^[a-zA-Z0-9]{3,10}$". Import jakarta.validation.constraints.Pattern.
Spring Boot
Need a hint?

Use the exact regex and place @Pattern above username.

4
Add getter and setter methods for email and username
Add public getter and setter methods for both email and username fields in the User class.
Spring Boot
Need a hint?

Write standard getter and setter methods for both fields.