Spring boot 国际化表单验证 @Validated 如何自定义资源文件夹?

Spring boot 表单验证 @Validated 的 message 国际化资源文件默认必须放在 resources/ValidationMessages.properties 中。

现在我想把资源文件放到 resources/i18n/validation/message.properties 中,请问该如何修改?

在网上找了点资料,尝试了下述代码,但还是不行

@Configuration
public class ValidationConfig {

    @Bean
    public Validator getValidator() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/validation/message.properties");

        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource);

        return validator;
    }

}
阅读 7.3k
2 个回答

这样配置即可

@Configuration
public class ValidationConfig {

    @Bean
    public Validator getValidator() {
        Validator validator = Validation.byDefaultProvider().
        configure().
        messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator("i18n/validation/message"))).
        buildValidatorFactory().getValidator();
        return validator;
    }

}
新手上路,请多包涵

messageSource.setBasename("i18n/validation/message.properties");
改为:
messageSource.setBasename("i18n/validation/message");

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题