Configure custom validation MessageSource in Spring Boot
While working with validation API, we may want to display custom or user friendly messages to the customer. In this post, we’ll see how to configure and manage custom validation MessageSource in the application using Spring Boot.
MessageSource is a powerful feature available in Spring applications. It is a strategy interface for resolving messages, with support for the parameterization and internationalization of such messages.
Spring provides two out-of-the-box implementations for production
- ResourceBundleMessageSource: built on top of the standard ResourceBundle
- ReloadableResourceBundleMessageSource: highly configurable, in particular with respect to reloading message definitions
Maven dependencies
Let’s start with adding the necessary Maven dependencies. Add the spring-boot-starter-web starter in your pom.xml to enable JSR 303 bean validation for your application. This is how our pom.xml looks like
Custom Validation Message
Consider an example of a Registration form consisting of three mandatory fields name, email and password together with a non mandatory field city. As part of custom validation message, if the user doesn’t provide the correct details as input, we need to show error messages to the end user according to the user’s locale.
Let’s add validation constraints to the registration form. The bean validator will read these constraints and return error in case input data is not meeting these constraints. Please find below the bean class.
As can be seen, here we have added validation constraints that verifies the input fields(name, email and password) in the form. To show custom and locale-specific message, we can provide a placeholder as mentioned for the annotations @NotBlank, @NotNull.
The placeholders name.not.blank, email.not.null, password.not.blank will be resolved by the MessageSource configuration.
The MessageSource Bean
Let’s define the MessageSource bean with detail about our resource bundle and encoding. You can define this bean in your application configuration class. We will use the ReloadableResourceBundleMessageSource that accesses resource bundles using specified base names
LocalValidatorFactoryBean
We need to register our MessageSource bean with the LocalValidatorFactoryBean to use our custom messages. We will define this bean in the same configuration class.
Locale Resolver
LocaleResolver is required to decide which local is currently used.
Defining Property Files
The next step is to define the property files for our application which will contain the locale specific messages. Spring will automatically take care of referring the correct property file based on the locale. You need to create these files under src/resource directory with the name provided in the basename. Remember to use the correct naming convention like messages_{locale}.properties.
Here we can take advantage of internationalization along with this. Let’s say we want to show messages for a German user as well in their language. In this case, we have two property files — messages.properties and messages_de.properties in the same location. This is how the property files will look like
messages.properties
name.not.blank=Please provide your name
email.not.null=Please provide a valid email address
password.not.blank=Please provide a valid password. Your password should contain at least 8 characters with at least 1 uppercase, lowercase and special character
messages_de.properties
name.not.blank=Bitte geben Sie Ihren Namen an
email.not.null=bitte geben Sie eine gültige E-Mail Adresse an
password.not.blank=Bitte geben Sie ein gültiges Passwort an. Ihr Passwort sollte mindestens 8 Zeichen mit mindestens 1 Groß-, Klein- und Sonderzeichen enthalten
With above steps, we have configured custom validation MessageSource in Spring Boot and we have seen how the default validation messages can be changed without modifying the code if the configuration is done properly beforehand.