Validate Email Address in Spring Boot

In this tutorial, you will learn how to use @Email and@Validated annotations to validate an email address in the Spring Boot application.

The value of the email address we will read from an HTTP GET request. If you are looking for an example of validating the email address sent in the HTTP POST request, please look at my tutorial on how to validate the request body in RESTful Web Service.

Reading Email Address with @RequestParam

When building RESTful Web Service applications with Spring Boot REST, we use the @RequestParam annotation to read the HTTP Request parameter sent in HTTP GET Request.

Below is an example of a URL with an email address as a query string parameter. To read an email address from a URL query string in our REST Spring Boot application, we will need to handle HTTP GET Request:

http://localhost:8011/account-ws/account/[email protected]

Below is a code example that uses @RequestParam annotation to read HTTP GET Request parameter.

@RestController
@RequestMapping("/account")
public class AccountController {
    
    @Autowired
    AccountService accountService;
 
    @GetMapping("/password-reset-request")
    public OperationStatusModel sendPasswordResetRequest(@RequestParam("email") String email) {
        OperationStatusModel returnValue = new OperationStatusModel();
        
        returnValue.setOperationName(RequestOperationName.REQUEST_PASSWORD_RESET.name());
        returnValue.setOperationResult(RequestOperationStatus.SUCCESS.name());
        
        boolean isSuccessfulRequest = accountService.requestPasswordReset(email);
        
        if(!isSuccessfulRequest)
        {
            returnValue.setOperationResult(RequestOperationStatus.ERROR.name());
        }
        
        return returnValue;
    }
}

Validate Email Address

To validate the email address in the Spring Boot application, we can use @Email and @Validated annotations.

In the code example below, we will read the email address from the HTTP Get request. Reading the email address sent in the HTTP POST request is different. If you need to validate the email address sent in the HTTP POST request, follow my other tutorial on validating request parameters sent in HTTP Request Body.

To validate the email address, we will use the following two annotations.

  • @Validated – Add this annotation above the class declaration,
  • @Email – Add this annotation next to @RequestParam

Code example

@RestController
@RequestMapping("/account")
@Validated
public class AccountController {
    
    @Autowired
    AccountService accountService;


    @GetMapping("/password-reset-request")
    public OperationStatusModel sendPasswordResetRequest(@Email @RequestParam("email") String email) {
        OperationStatusModel returnValue = new OperationStatusModel();
        
        returnValue.setOperationName(RequestOperationName.REQUEST_PASSWORD_RESET.name());
        returnValue.setOperationResult(RequestOperationStatus.SUCCESS.name());
        
        boolean isSuccessfulRequest = accountService.requestPasswordReset(email);
        
        if(!isSuccessfulRequest)
        {
            returnValue.setOperationResult(RequestOperationStatus.ERROR.name());
        }
        
        return returnValue;
    }
}

I hope this short tutorial was helpful.

If you want to learn more about how to build RESTful Web Services with Spring Boot REST, check my other step-by-step Spring Boot tutorials, many of which have video lessons. And if you enjoy learning by watching step-by-step video lessons, look at the below list of video courses that teach Spring Boot.


Leave a Reply

Your email address will not be published. Required fields are marked *