简体   繁体   English

Spring Data Rest Bean验证国际化

[英]Spring data rest Bean Validation Internationalization

I'm new to spring specially spring boot, i'm trying to use internationalisation, so far i'm using with normal controllers fine, but i can't make it work with Bean Validation using spring-data-rest. 我是春季特别是春季引导的新手,我正在尝试使用国际化,到目前为止,我与普通控制器配合使用都很好,但是我不能使其与使用spring-data-rest的Bean验证一起使用。

I'm using the following code to start the application: 我正在使用以下代码启动该应用程序:

@EnableJpaRepositories
@SpringBootApplication
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    @Bean(name = "validator")
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

I can use locale fine with Controllers, but i can't make Bean Validation respect other locales, it always use default locale. 我可以在Controllers中使用区域设置,但是我不能使Bean验证尊重其他区域设置,它始终使用默认区域设置。

Any help? 有什么帮助吗?

I'm using spring-data-rest and spring-boot in version 1.2.5.RELEASE. 我在1.2.5.RELEASE版本中使用spring-data-rest和spring-boot。

I found a solution, since no one helped i'm posting because can help someone else. 我找到了解决方案,因为没有人帮助我发布信息,因为可以帮助其他人。

If i don't set the default location on SessionLocaleResolver it gets the locale from Accept-Header so i'll work. 如果我没有在SessionLocaleResolver上设置默认位置,它将从Accept-Header获取语言环境,因此我可以工作。

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    // Remove this line
    // slr.setDefaultLocale(Locale.US); 
    return slr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM