Search code examples
javaspringvalidation

@UUID hibernate validator doesn't validate on application start


I try to use a validation on UUIDs using Spring.

When I use a Pattern directly at the configuration class as shown in the code and type in a wrong UUID in the yaml it correctly prints an error on the validation at startup of my application:

@Validated
@Configuration
@Getter
@Setter
@ConfigurationProperties
public class ClientConfig {

    @Pattern(regexp = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
    @Nonnull
    private String clientId;

}

If I use the answers provided here and extract the pattern into a self implemented @interface it works as well. But if I try to use the annotation from hibernate, so switching to this:

    @org.hibernate.validator.constraints.UUID
    @Nonnull
    private String clientId;

Then my application doesn't validate the property in the yaml anymore and won't fire any errors on startup.

What am I missing for hibernate? Do I need to register some extra annotation processor to get it working and how?

I am a bit new to the @interfaces, also I don't get how to easily find the implementation classes behind the interface.


Solution

  • I found out the following:

    Using jdk11

    I get no compilation errors in the IDE and I can start the application and it does not halt at any places inside the UUIDValidator. However if I try to run mvn install I get the following error:

    [ERROR]     package m365.graph.client;
    [ERROR]     ^
    [ERROR] The type jakarta.validation.Payload cannot be resolved. It is indirectly referenced from required .class files
    

    Which is not the config class itself but a class using the config.

    Using jdk17

    Since we wanted to move to 17 anyways I finished the upgrade and tried it here as well, since I knew that you can't use javax.annotation there anymore. And in this case everything works as expected. It correctly stops at the initilize breakpoint in the UUIDValidator and also in the config a wrong UUID gets validated throwing an error.