Spring Boot – Validate Email in @RequestParam

In this short Spring Boot tutorial I am going to show you how use @Validated and @Email annotations to validate email address sent to a RESTful Web Service in HTTP GET Request. If you are looking for an example of how to validate email address sent in HTTP POST request, please have a look at my tutorial on how to validate request body in RESTful Web Service.

Reading Request Parameters with @RequestParam

When building RESTful Web Service applications with Spring MVC and Spring Boot we use @RequestParam to read HTTP Request parameter sent in HTTP GET Request. Below is an example of URL that includes email address in a query spring. To read 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 Request Parameter

To validate email address sent to our RESTful Web Service in HTTP GET Request we can use @Email and @Validated annotations. Validating email address sent in HTTP POST request is different. If you need to validate email address sent in HTTP POST request follow my other tutorial on how to validate request parameters sent in HTTP Request Body.

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

Below is a code example that uses @Validated and @Email annotations to validate email address.

@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 are interested to learn more about how to build RESTful Web Services with Spring MVC and Spring Boot check my other step by step tutorials many of which have video tutorials as well. And if you enjoy learning by watching step by step video lessons then have a look at the below list of video courses that teach Spring Boot.