Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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/5/spring-mvc/2.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为类级约束验证器启用国际化_Spring_Spring Mvc_Spring Boot_Hibernate Validator - Fatal编程技术网

使用Spring为类级约束验证器启用国际化

使用Spring为类级约束验证器启用国际化,spring,spring-mvc,spring-boot,hibernate-validator,Spring,Spring Mvc,Spring Boot,Hibernate Validator,我正在使用SpringBoot、SpringJPA、SpringDataREST、SpringHateOAS、HibernateValidator。 我创建了自己的约束验证器 注释: Target({ java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.ANNOTATION_TYPE }) @Retention(RetentionPolicy.RUNTIME) @Constraint(valida

我正在使用SpringBoot、SpringJPA、SpringDataREST、SpringHateOAS、HibernateValidator。 我创建了自己的约束验证器

注释:

    Target({ java.lang.annotation.ElementType.TYPE, java.lang.annotation.ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { TicketBundleValidator.class })
@Documented
public @interface ValidTicketBundle {

    String message() default "{server.validators.annotations.ValidTicketBundle.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
我创建了测试用例,以验证是否一切正常:

 @Test
    @WithMockUser(roles = "ADMIN")
    public void saveTicketBundleWithWrongDateThrowsException() {
        TicketBundle ticketBundle = new TicketBundle();
        ticketBundle.setCustomer(customer);
        ticketBundle.setStartDate(Instant.now().plus(30, ChronoUnit.DAYS));
        ticketBundle.setEndDate(Instant.now());

        Set<ConstraintViolation<TicketBundle>> constraintViolations = validator.validate(ticketBundle);
        assertEquals(1, constraintViolations.size());
        ConstraintViolation<TicketBundle> constraintViolation = constraintViolations.iterator().next();
        assertEquals("I dati inseriti non sono validi. Verificare nuovamente e ripetere l'operazione.",
                constraintViolation.getMessage());
    }

我在代码中发现了问题。我错了,hibernate validator在根文件夹
validationmessages.properties
()


我在那里设置了消息,一切正常。

在这个上下文中消息源是如何引用的?@RomanC我以为是Spring Boot自动引用的。嘿@drenda我问了你一个问题?如果你不能回答这个问题,你应该提供一个清晰的问题陈述,包括能够重现问题的代码,如果没有清晰的语句和限定的代码,问题就脱离主题,应该尽快删除,以便在不了解详细信息的情况下为您提供解决方案,或者删除您的帖子,以免干扰开发人员试图争论,而不是提供详细信息。@RomanC类消息源在此上下文中未被引用,无法对您的问题做出适当的响应。但是,你可以简单地避免回答,让知道如何回答的其他用户有空间,而不是要求删除一个记录良好的问题。该问题可能对其他有相同问题的用户有用。谢谢
server.validators.annotations.ValidTicketBundle.message = I dati inseriti non sono validi. Verificare nuovamente e ripetere l'operazione.
 @Test
    @WithMockUser(roles = "ADMIN")
    public void saveTicketBundleWithWrongDateThrowsException() {
        TicketBundle ticketBundle = new TicketBundle();
        ticketBundle.setCustomer(customer);
        ticketBundle.setStartDate(Instant.now().plus(30, ChronoUnit.DAYS));
        ticketBundle.setEndDate(Instant.now());

        Set<ConstraintViolation<TicketBundle>> constraintViolations = validator.validate(ticketBundle);
        assertEquals(1, constraintViolations.size());
        ConstraintViolation<TicketBundle> constraintViolation = constraintViolations.iterator().next();
        assertEquals("I dati inseriti non sono validi. Verificare nuovamente e ripetere l'operazione.",
                constraintViolation.getMessage());
    }
 org.junit.ComparisonFailure: expected:<[I dati inseriti non sono validi. Verificare nuovamente e ripetere l'operazione.]> but was:<[{server.validators.annotations.ValidTicketBundle.message}]>
    at org.junit.Assert.assertEquals(Assert.java:115)
@Configuration
@EnableHypermediaSupport(type = { HypermediaType.HAL })
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
 @Bean
public LocaleResolver localeResolver() {
    return new SmartLocaleResolver();
}

public class SmartLocaleResolver extends CookieLocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String acceptLanguage = request.getHeader("Accept-Language");
        if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {
            return super.determineDefaultLocale(request);
        }
        return request.getLocale();
    }
}

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/i18n/messages");
    // messageSource.setDefaultEncoding("UTF-8");
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setCacheSeconds((int) TimeUnit.HOURS.toSeconds(1));
    messageSource.setFallbackToSystemLocale(false);
    return messageSource;
}