I'm trying to add validation to a bean so I can verify its state before persisting it.
The class for the bean that requires validation is generated by the openapi-generator-maven-plugin plugin from an OpenAPI contract.
As the contract contains restrictions, they Jakarta constraint annotations are present on the generated bean as well. It looks like this (simplified):
public class User {
@NotNull
private String name;
}
I added a messages.properties file in the src/main/resources folder with the following content:
jakarta.validation.constraints.NotNull.message=global not null error message
If I then execute the jakarta.validation.Validator.validate(user), the interpolated message matches the configured one. But this configuration applies to all properties that are null, while I only want to apply this to this specific property.
Is there a way I can configure the bean validator to return a specific message through configuration, without having to modify the bean (as it is generated)? I tried to add the following to the messages.properties (suggested by AI):
org.company.User.name.NotNull=Username cannot be null
But the validation framework just seems to ignore this?
All help is appreciated.