Home > Java > javaTutorial > body text

How to configure SpringBoot Validation prompt information internationalization

PHPz
Release: 2023-05-11 11:43:05
forward
1402 people have browsed it

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:

How to configure SpringBoot Validation prompt information internationalization

##Default

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:

How to configure SpringBoot Validation prompt information internationalization

In our actual use process, in addition to setting

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 ##.

How to configure SpringBoot Validation prompt information internationalizationSpring 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

In Spring Boot 2.5.x version, Spring Boot Validation only supports reading the international code in the

resources/ValidationMessages.properties

series of files by default. Internationalization attribute, and Chinese requires ASCII transcoding to display correctly as shown below:

How to configure SpringBoot Validation prompt information internationalization 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:

How to configure SpringBoot Validation prompt information internationalization 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;
    }

}
Copy after login

Note:

    After configuring through the above configuration class,
  • Only compatible with the internationalization files set by Spring Boot spring.messages and the internationalization files of the original hibernate-validator
  • The resource/ValidationMessages.properties series is no longer supported
  • Spring Boot 2.6.x

I looked through the Release instructions of the Spring Boot framework on Github and found that it was mentioned in v2.6.0-M2 released on 2021-08-20 It is now supported to use its own internationalized MessageSource to parse verification prompt information. That is to say, Spring Boot 2.6.x and later supports the verification annotation message attribute to reference Spring Boot's own internationalized configuration.

How to configure SpringBoot Validation prompt information internationalizationActually test the Spring Boot 2.6.x version and verify the annotation

message="{propName}"

:

    You can directly read the properties in the internationalization configuration set through
  • spring.messages

    (Chinese does not require ASCII transcoding after setting UTF-8 encoding),

  • Also supports reading the configuration in
  • resources

    /ValidationMessages.properties (Chinese requires ASCII transcoding),

  • and still supports the original Internationalization files for
  • hibernate-validator

    .

  • And the read priority is from top to bottom, that is, the top one takes effect first.

The specific configuration and usage are shown in the figure below:

How to configure SpringBoot Validation prompt information internationalizationSpring 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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template