Reputation: 3214
I am trying to validate a emailId which is part of a method parameter and a Query param for the rest endpoint. Using the @Pattern annotation in the method parameter is not working. Is there a better way to handle this scenario using annotation
public EmailResponse isEmailRegistered(HttpServletRequest request,
@RequestParam("emailId") @Valid @Pattern(message = "Email is invalid", regexp = "^.+@.+\\..+$") final String emailId)
I tried the above and it does not work. Because the email Id field is query parameter I cannot wrap it in a object and add validator on it.
I tried wrapping it in a wrapper object as below but to no luck
public EmailResponse isEmailRegistered(HttpServletRequest request,
@RequestParam("emailId") @Valid final EmailIdWrapper emailId, final BindingResult bindingResult)
EmailId Wrapper class
public class EmailIdWrapper implements Serializable{
@NotNull(message = "Email Id is required")
@Size(min = 5, max = 50, message = "Invalid length for email")
@Pattern(message = "Invalid email id", regexp = "^.+@.+\\..+$")
private String emailId;
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
With this case I get a conversionnotsupported error which is quite obvious as the url parameter is a string and I am assigning to a object.
Upvotes: 2
Views: 21812
Reputation: 403
I know it's a bit late, but have you tried putting
@Validated
in the Controller class level?
Upvotes: 1