Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 春季四,;Hibernate 5验证程序消息源不工作_Spring_Hibernate_Spring Mvc_Hibernate Validator - Fatal编程技术网

Spring 春季四,;Hibernate 5验证程序消息源不工作

Spring 春季四,;Hibernate 5验证程序消息源不工作,spring,hibernate,spring-mvc,hibernate-validator,Spring,Hibernate,Spring Mvc,Hibernate Validator,我无法使用SpringMVC4.2.5.RELEASE使用HibernateValidation5.1.0.Final 我的网络配置: @Bean public LocalValidatorFactoryBean validator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSour

我无法使用SpringMVC4.2.5.RELEASE使用HibernateValidation5.1.0.Final

我的网络配置:

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource());
        return validator;
    }
    @Bean
    @Autowired
    public MethodValidationPostProcessor getValidationPostProcessor(LocalValidatorFactoryBean validator) {
        MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
        processor.setValidator(validator);
        return processor;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }

    @Override
    public Validator getValidator() {
        return validator();
    }
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
        LocaleChangeInterceptor l = new LocaleChangeInterceptor();
        l.setParamName("lang");
        return l;
    }

    @Bean
    public SessionLocaleResolver localeResolver(){
        SessionLocaleResolver s = new SessionLocaleResolver();
        s.setDefaultLocale(Locale.ENGLISH);
        return s;
    }
我有一个ExceptionHander,它获取验证消息并将其作为json推回:

    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public ValidationError handleConstraintViolation(final ConstraintViolationException exception) {
        ValidationError v = new ValidationError();

        exception.getConstraintViolations().forEach(violation -> {
                v.addError(violation.getPropertyPath().toString(), violation.getMessage());
        });
        logger.warn(exception, exception);
        return v;
    }
我在src/main/resources/i18n/中有3个文件:messages\u en\u en.properties、messages\u pl\u pl.properties、messages.properties。 已验证的模型类有一个已验证的参数:

    @Column(name = "VALUE_", nullable = false)
    @Email(message = "{Email.contractorContactEmail.value}")
    @NotNull(message = "{NotNull.contractorContactEmail.value}")
    private String value;
我看到的是hibernate验证器查看的是classpath:ValidationMessages属性,而不是我的spring消息源。这对我来说可能没问题,但Hibernate不想翻译这些消息-区域设置始终是服务器默认设置。我做错了什么??我怎样才能修好它

PS.在控制器中,我使用@org.springframework.validation.annotation.Validated

PS2。我确信我的messageSource工作正常,因为如果我将此代码添加到ExceptionHandler中,它的翻译会非常完美,但我知道这是一种不好的做法

exception.getConstraintViolations().forEach(violation -> {
            String messageTemplate = violation.getMessageTemplate();
            if (messageTemplate.startsWith("{") && messageTemplate.endsWith("}")) {
                String key = StringUtils.substring(messageTemplate, 1, messageTemplate.length() - 1);
                v.addError(violation.getPropertyPath().toString(), messageSource.getMessage(key, null, LocaleContextHolder.getLocale()));
            }
});

我可以马上告诉您的是,从您的配置判断,您的消息位于错误的位置-您已将消息源配置为查看
i18/messages
,但您的消息位于类路径根目录中。因此,您的消息源定义应该如下所示:

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setUseCodeAsDefaultMessage(true);
    return messageSource;
}
除此之外,其余的配置看起来都很好。我猜您正在使用
WebMVCConfigureAdapter

编辑:
供将来读者参考,该问题与配置无关,JPA实体在
@OneToMany
字段上缺少
@Valid
注释,因此控制器级验证无法将其提取出来,Hibernate的JPA验证器使用了默认消息。

对不起,我的错误。我的消息在src/main/resources/i18n中。我与它斗争了3天,无法配置它。。。我在想,这是否是Spring4和HibernateValidator5之间集成的问题?我正在使用jboss作为web应用服务器。。也许会有帮助?也许我丢了一些罐子?我不知道..不,如果您的消息配置正确,那么我猜MVC没有正确地拾取
LocalValidatoryBean
。您可以尝试添加一个
@Autowired私有localvalidatoryfactorybean验证器字段输入到配置类中,并从
getValidator()
返回字段值。是的,我正在使用WebMVCConfigureAdapter类。是的,我尝试在ExceptionHandling类中自动连接LocalValidatorFactoryBean,它从配置中返回bean。此外,由于LocaleEconTextHolder.getLocale()返回正确的值,所以区域设置更改也可以工作。您知道我可以在哪个入口点调试hibernate以查看可能的问题吗?