Spring Boot Validation
Usages
UUID Validataion
Define a UUID Validataion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import javax.validation.Constraint;import javax.validation.Payload;import javax.validation.constraints.Pattern;import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;import static java.lang.annotation.RetentionPolicy.RUNTIME;@Pattern(regexp="^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", message = "Not a valid UUIDPattern") @Retention(RUNTIME) @Target({FIELD, METHOD, PARAMETER}) @Constraint(validatedBy = {}) @Documented public @interface UUIDPattern { String message () default "{invalid.uuid}" ; Class<?>[] groups() default {}; Class<? extends Payload >[] payload() default {}; }
Example
1 2 3 4 5 6 public class User { @Id @GeneratedValue @UUIDPattern protected UUID id; }
java - Is there a uuid validator annotation? - Stack Overflow - https://stackoverflow.com/questions/37320870/is-there-a-uuid-validator-annotation
Java validator for UUIDs - https://gist.github.com/mindhaq/ffc3378a271cb7e91b3819ff1abb675c
FAQs
HV000030: No validator could be found for constraint ‘javax.validation.constraints.Pattern’ validating type ‘java.util.UUID’.
As stated in problem, to solve this error you MUST use correct annotations. In above problem, @Pattern
annotation must be applied on any String
field only.
TODO.
References
Validation with Spring Boot - the Complete Guide - Reflectoring - https://reflectoring.io/bean-validation-with-spring-boot/
[1] Validation in Spring Boot | Baeldung - https://www.baeldung.com/spring-boot-bean-validation