Search code examples
javaspringspring-bootvalidationjavax.validation

How to set @NotEmpty annotation messages in Spring?


In a Java (+ Spring Boot) project, there is a notation using javax.validation as shown below:

@NotEmpty(message = "validation.product.notEmpty")
private String product;

@NotEmpty(message = "validation.username.password")
private String password;

I have a look at the usage of them, but there are some points that I could not understand:

1. Is there a special usage e.g. conditional message displaying for validation.username.password? For example if username field is null, then display this message? Or is it completely the same manner as the product field?

2. I search the project, but could not find validation.product.notEmpty or validation.username.password. So, how do they work? I think there should be a definition for these messages, but as I did not find, is it come from default messages of javax.validation?


Solution

    1. What is the difference between @EmailRegex and @Email? And is there any need to also use @NotEmpty with these @EmailRegex or @Email annotations?

    @Email will not throw error for an empty String. So you need @NotEmpty to be sure that this String is not empty if you always require an email to be there.

    @Email will consider valid everything that is in the form [email protected]. If you want to further constraint this you can use @EmailRegex so that you allow only [email protected] by defining your own regular expression.

    @EmailRegex does not seem to be included in hibernate annotations or spring annotations. So it is either a custom annotation imported from somewhere else or just a custom annotation of your application. Inspect the code to see how it actually behaves but from it's name I suppose it behaves as I have explained above.

    1. I search the project, but could not find validation.product.notEmpty or validation.username.password. So, how do they work? I think there should be a definition for these messages, but as I did not find, is it come from default messages of javax.validation?

    It should be with {....} so like @NotEmpty(message = "{validation.username.password}") private String password;. In that case Spring will automatically read properties from the property files and apply the value for the property validation.username.password. If it does not exist then go to either application.properties or application.yaml and add that property.

    Some more notes on this last one. I have seen some strange cases in backend-frontend applications which might be your case here.

    @NotEmpty(message = "validation.username.password")
    

    The actual message thrown here when the validation fails is validation.username.password. I have seen cases where the frontend then reads that message and binds a value to this one. I have seen this to be used when frontend supports multiple languages and binds another value for each language each time. This would explain why you don't have { } or such a property in your application.

     @NotEmpty(message = "{validation.username.password}")
    

    with an existing property validation.username.password= password can not be empty will have as a result when the validation fails the message password can not be empty to be delivered.