I've got a simple REST resource which accepts a couple of query parameters. I'd like to validate one of these parameters, and came across ConstraintValidator
for this purpose. The REST resource expects the query param territoryId
to be a UUID
, so I'd like to validate that it indeed is a valid UUID
.
I've created an @IsValidUUID
annotation, and a corresponding IsValidUUIDValidator
(which is a ConstraintValidator
). With what I have now, nothing gets validated and getSuggestions
accepts anything I throw at it. So clearly I'm doing something wrong.
What am I doing wrong?
The REST resource now looks like this :
@Component
@Path("/search")
public class SearchResource extends AbstractResource {
@GET
@Path("/suggestions")
@Produces(MediaType.APPLICATION_XML)
public Response getSuggestions(
@QueryParam("phrase") List<String> phrases,
@IsValidUUID @QueryParam("territoryId") String territoryId) {
[...]
}
}
IsValidUUID
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = {IsValidUUIDValidator.class})
public @interface IsValidUUID {
String message() default "Invalid UUID";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
IsValidUUIDValidator
public class IsValidUUIDValidator implements ConstraintValidator<IsValidUUID, String> {
@Override
public void initialize(IsValidUUID constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
try {
UUID.fromString(value);
return true;
} catch (Exception e) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("The provided UUID is not valid")
.addConstraintViolation();
return false;
}
}
}