Reputation:
i'm trying to do a validation of only positive numbers inside the request in spring boot
Public ResponseEntity getId(@RequestParam(value =“idPer”)@Pattern(regexp=“^[0-9]*$”),message = “the value is negative”)
@NotNull(message = “the value is empty ”) Long idPer){
}
in my exceptionhandler I get this exception
no validator could be found for constraint 'javax.validation.constraints.pattern' validatingtype long
I have the @validated above my controller class
what am I doing wrong?
Upvotes: 5
Views: 17100
Reputation: 785196
Check documentation of @Pattern
. It is applicable to CharSequence
i.e. String
bur not allowed for Long
which is what you are using in your method.
You may refactor your code like this:
public ResponseEntity getId (
@Pattern(regexp="^[0-9]+$", message="the value must be positive integer")
@RequestParam("idPer") final String idPerStr) {
Long idPer = Long.valueOf(idPerStr);
// rest of your handler here
}
Also note that @NotNull
is not required because we are using +
quantifiers that allows only 1 or more digits in pattern.
Upvotes: 5