• Spring Boot Validation提示信息国际化配置


    引言

    之前介绍过Spring Boot Validation的使用及扩展,可参见:《SpringBoot Validation》
    本文在此基础上重点讲解下Spring Boot Validation如何集成国际化。

    Spring Boot Validation支持JSR-380(aka. Bean Validation 2.0,part of Jakarta EE and JavaSE)注解,可通过验证注解的message属性设置验证错误提示信息,且每个验证注解都有默认的message配置,例如@NotBlank的message属性值设置如下图:

    在这里插入图片描述
    默认的message = "{...}"的形式即指定国际化属性的名称,后续会根据语言环境替换为对应的值,而这些国际化属性的定义可参见hibernate-validator中的org.hibernate.validator.ValidationMessages.properties等一些列国际化属性定义:

    在这里插入图片描述
    而我们在实际使用过程中,除了可以设置message="{propName}"的形式来读取国际化属性,亦可以直接设置值message="具体的提示信息",而不明确设置message则使用之前提到的org.hibernate.validator.ValidationMessages.properties中的默认配置。

    在这里插入图片描述

    Spring Boot Validation起初其并不直接支持读取Spring Boot自身的国际化配置(通过spring.messages进行配置),而是需要在resources/ValidationMessages.properties中进行配置的国际化属性才会生效,而后续在Spring Boot 2.6+版本才支持Validation与Spring Boot自身的国际化配置相结合。接下来结合Spring Boot 2.5和2.6版本分别介绍下Spring Boot Validation如何集成自定义国际化验证提示信息。

    Spring Boot 2.5.x

    在Spring Boot 2.5.x版本中,Spring Boot Validation默认只支持读取resources/ValidationMessages.properties系列文件的中的国际化属性,且中文需要进行ASCII转码才可正确显示,如下图:
    在这里插入图片描述
    而就算Spring Boot应用亦声明了自身的国际化配置,但是Spring Boot Validation框架是读取不到的,
    例如Spring Boot应用自身的国际化配置如下图,但是在验证注解message属性设置i18n/messages.properties中的"{propName}"是读取不到的:
    在这里插入图片描述

    而想要Spring Boot Validation框架和Spring Boot自身使用同样的国际化配置,则可通过如下方式进行配置:

    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;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    注:
    通过上述配置类进行配置后,

    • 仅兼容Spring Boot spring.messages设置的国际化文件和原hibernate-validator的国际化文件
    • 不再支持resource/ValidationMessages.properties系列

    Spring Boot 2.6.x

    翻了Github上Spring Boot框架的Release说明,发现其在2021-08-20发布的v2.6.0-M2中提到了支持使用自身国际化MessageSource解析验证提示信息,也就是说Spring Boot 2.6.x版本之后已支持验证注解message属性引用Spring Boot自身国际化配置。
    在这里插入图片描述
    实际测试Spring Boot 2.6.x版本,验证注解message="{propName}"

    • 可以直接读取通过spring.messages设置的国际化配置中的属性(设置UTF-8编码后中文可无需ASCII转码),
    • 同时亦支持读取resources/ValidationMessages.properties中的配置(中文需ASCII转码),
    • 且依旧支持原hibernate-validator的国际化文件

    且读取优先级依次从上到下,即最上面的优先生效。
    具体的配置及使用方式见下图:
    在这里插入图片描述

    Spring Boot 2.6关于Spring Boot Validation与MessageSource的融合,具体实现细节可参见Release中提到的GitHub Pull Request #17530
    在这里插入图片描述


    参考:

    https://github.com/spring-projects/spring-boot/releases/tag/v2.6.0-M2
    https://github.com/spring-projects/spring-boot/pull/17530
    https://github.com/spring-projects/spring-boot/pull/17530/commits/0cbb3465386484f3ca27c3d7842f000acfa5e335
    https://stackoverflow.com/questions/45692179/spring-boot-validation-message-is-not-being-resolved

  • 相关阅读:
    基于matlab目标雷达横截面建模(附源码)
    Spring学习笔记注解式开发(3)
    2 找出从指定结点出发且长度为m的所有简单路径---来源舒姐
    PAT数字&字符串处理
    Windows与Ubuntu之间的文件传输
    url后面直接拼接参数
    rasa 对话机器人--http rest api
    ffmpeg常用命令(未完待续)
    Spring Boot插件化开发概念原理及实现
    字符串与字符编码 - GO语言从入门到实战
  • 原文地址:https://blog.csdn.net/luo15242208310/article/details/124897389