Spring Boot Validation supports JSR-380 (aka. Bean Validation 2.0, part of Jakarta EE and JavaSE) annotations. The verification error message can be set through the message attribute of the verification annotation, and each verification annotation has a default message configuration. , for example, the message attribute value of @NotBlank
is set as shown below:
message = "{...}" The form specifies the name of the internationalization attribute, which will be replaced with the corresponding value according to the locale. For the definition of these internationalization attributes, please see
org.hibernate.validator in hibernate-validator
. ValidationMessages.properties and a series of international property definitions:
message="{propName}" to read the international properties, you can also directly set the value message="specific prompt information". If you do not explicitly set the message, use the previously mentioned
org.hibernate.validator.ValidationMessages.properties# Default configuration in ##.
Spring Boot Validation initially does not directly support reading Spring Boot’s own international configuration (configured through spring.messages), but needs to be configured in resources/ValidationMessages. Only the internationalization properties configured in properties will take effect, and the combination of Validation and Spring Boot's own internationalization configuration will be supported later in Spring Boot 2.6.
Next, combined with Spring Boot 2.5 and 2.6 versions, we will introduce how Spring Boot Validation integrates custom international verification prompt information.
Spring Boot 2.5.x
series of files by default. Internationalization attribute, and Chinese requires ASCII transcoding to display correctly as shown below:
Even the Spring Boot application also declares its own internationalization Configuration, but the Spring Boot Validation framework cannot read it
For example:
The international configuration of the Spring Boot application itself is as shown below, but the verification annotation message property is set to i18n/messages.properties The
"{propName}" cannot be read:
and you want the Spring Boot Validation framework and Spring Boot itself to use the same international configuration, you can configure it in the following way:
import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * Spring Web验证器自定义国际化文件配置<br/> * 注:适用于Spring Boot 2.5.* * * @author luohq * @date 2022-05-21 */ @Configuration public class WebValidationConfig implements WebMvcConfigurer { /** * 国际化消息源 */ private MessageSource messageSource; public WebValidationConfig(MessageSource messageSource) { //注入Spring Boot国际化消息源(需通过spring.messages明确指定) this.messageSource = messageSource; } /** * 使用自定义LocalValidatorFactoryBean, * 设置Spring国际化消息源 */ @Bean @Override public Validator getValidator() { LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); //仅兼容Spring Boot spring.messages设置的国际化文件和原hibernate-validator的国际化文件 //不支持resource/ValidationMessages.properties系列 bean.setValidationMessageSource(this.messageSource); return bean; } }
Actually test the Spring Boot 2.6.x version and verify the annotation
message="{propName}":
(Chinese does not require ASCII transcoding after setting UTF-8 encoding),
/ValidationMessages.properties
(Chinese requires ASCII transcoding),
.
The specific configuration and usage are shown in the figure below:
Spring Boot 2.6 Regarding the integration of Spring Boot Validation and MessageSource, the specific implementation details can be found in Release GitHub Pull Request #17530 mentioned:
The above is the detailed content of How to configure SpringBoot Validation prompt information internationalization. For more information, please follow other related articles on the PHP Chinese website!