Sub topic 10
Customizing and externalizing messages
This chapter assumes you can declare constraints on a DTO, trigger them with @Valid, and return field errors from a @RestControllerAdvice. It covers how to replace the default messages and move them into property files.
Inline messages
Every constraint has a message attribute. Without it, Hibernate Validator supplies an English default such as must not be blank. You can override the text directly:
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public record CreateUserRequest(
@NotBlank(message = "Name is required")
@Size(max = 50, message = "Name must have at most {max} characters")
String name,
@NotBlank @Email(message = "Enter a valid email address") String email) {
}The {max} placeholder is replaced with the value of the constraint's own max attribute. Other attributes work the same way, for example {min} on @Min. Text inside ${...} is evaluated as an expression, such as ${validatedValue}, which prints the rejected value. Avoid echoing user input back in messages unless you are sure it is safe to show.
Externalizing messages
Hard-coded text is difficult to translate and to review. Instead, put a key in braces in the message attribute and define the text in a properties file.
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public record RegisterRequest(
@NotBlank(message = "{user.name.required}")
@Size(max = 50, message = "{user.name.size}")
String name) {
}Define the keys in src/main/resources/messages.properties:
user.name.required=Name is required
user.name.size=Name must have at most {max} charactersSpring Boot 2.6 and later passes the application's MessageSource to the validator. Your validation keys therefore live in the same messages.properties file that Spring Boot already reads (the spring.messages.basename default is messages). Hibernate Validator also reads a file named ValidationMessages.properties on the classpath. Use that file if you need messages that work outside Spring.
The text is resolved when validation runs, so getDefaultMessage() in your exception handler already returns the final sentence. You do not need to look up the key again.
Translating by locale
Add one file per language, named messages_<language>.properties:
user.name.required=Name ist erforderlich
user.name.size=Der Name darf höchstens {max} Zeichen lang seinSave this as messages_de.properties. Spring MVC resolves the locale from the Accept-Language request header by default. A request with Accept-Language: de therefore receives the German text, and other requests fall back to messages.properties.
Overriding the built-in defaults
Constraints without a message attribute use keys such as jakarta.validation.constraints.NotBlank.message. You can define that key in messages.properties to change the wording for the whole application:
jakarta.validation.constraints.NotBlank.message=This field is requiredCommon problems
- The response shows
{user.name.required}. The key is missing or misspelled, or the properties file is not on the classpath. Check the file name and its location undersrc/main/resources. - Placeholders are not replaced. Use
{max}for attributes and${validatedValue}for expressions. Attribute names must match the constraint's attributes exactly. - A translation is ignored. The client may not send
Accept-Language, or the language file does not exist. The fallback is the default file. - A custom constraint has no message. The
message()default in your annotation must also be a key or literal text, for example"{country.unsupported}".
Exercise
A @Size(min = 3, max = 20, message = "{user.name.size}") constraint should report "Name must be between 3 and 20 characters". Which line goes in messages.properties, and what changes if you add messages_de.properties?
Show answer
The line is user.name.size=Name must be between {min} and {max} characters. The placeholders are filled from the constraint's attributes. Adding messages_de.properties with the same key and German text makes requests that send Accept-Language: de receive the German message. All other requests still get the text from messages.properties.