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;
}
相关推荐
yoyo_zzm几秒前
Laravel6.x新特性全解析
java·spring boot·后端
xiaobaoyu10 分钟前
ssm
后端
Nick_zcy12 分钟前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi
源码宝15 分钟前
基于 SpringBoot + Vue 的医院随访系统:技术架构与功能实现
java·vue.js·spring boot·架构·源码·随访系统·随访管理
王中阳Go24 分钟前
2026年了,还在纠结后端转AI要不要死磕Python?试试Go吧
后端·go·ai编程
用户83562907805135 分钟前
用 Python 轻松在 Excel 工作表中应用条件格式
后端·python
red1giant_star39 分钟前
Python根据文件后缀统计文件大小、找出文件位置(仿Everything)
后端·python
长大19881 小时前
每秒10万写入的订单系统:MySQL分库分表、缓冲设计、批量写入优化实战
后端
渐儿1 小时前
缓存一致性与分布式锁:工程踩坑全解
后端
长大19881 小时前
为什么我加了索引,查询反而更慢了?
后端