Spring国际化语言切换不生效

使用SessionLocaleResolver一致不生效,获取message时候报错:

java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy

at org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver.setLocale...

org.springframework.web.servlet.i18n.LocaleChangeInterceptor.preHandle...

最终定位到原因,DispatcherServlet读取到的仍然是AcceptHeaderLocaleResolver,注入了但没有生效,问题代码如下:

java 复制代码
@Bean
@Primary
public LocalResolver sessionLocaleResolver() {
     SessionLocaleResolver slr = new SessionLocaleResolver();
     slr.setDefaultLocale(Locale.ENGLISH);
     slr.setDefaultTimeZone(TimeZone.getTimeZone("UTC"));
     return slr;
}

修改后代码如下:

java 复制代码
@Bean("localeResolver")
   @Primary
   public LocalResolver sessionLocaleResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.ENGLISH);
        slr.setDefaultTimeZone(TimeZone.getTimeZone("UTC"));
        return slr;
}
相关推荐
侠客行03173 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪3 小时前
深入浅出LangChain4J
java·langchain·llm
Victor3563 小时前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor3563 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
后端
灰子学技术5 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
老毛肚5 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎5 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码6 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚6 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂6 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言