Spring MVC 中解决中文乱码问题

在 Spring MVC 中解决中文乱码问题,需要从 请求参数编码响应内容编码 两方面入手。以下是完整的解决方案:


一、解决请求参数中文乱码

1. POST 请求编码(表单提交)

配置 CharacterEncodingFilter

web.xml 中添加 Spring 提供的字符编码过滤器,强制请求和响应使用 UTF-8 编码:

xml 复制代码
<!-- 解决 POST 请求中文乱码 -->
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param> 
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
2. GET 请求编码(URL 参数)

修改 Tomcat 的 server.xml

在 Tomcat 的 conf/server.xml 文件中,找到 Connector 配置,添加 URIEncoding="UTF-8"

xml 复制代码
<Connector 
    port="8080" 
    protocol="HTTP/1.1"
    URIEncoding="UTF-8"  <!-- 关键配置 -->
    connectionTimeout="20000"
    redirectPort="8443" />

二、解决响应内容中文乱码

1. 配置消息转换器(JSON 响应)

在 Spring MVC 配置文件中(如 spring-mvc.xml),设置 MappingJackson2HttpMessageConverter 的默认编码:

xml 复制代码
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/plain;charset=UTF-8</value>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
2. 全局响应编码配置

web.xml 中添加响应编码过滤器:

xml 复制代码
<filter>
    <filter-name>responseFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>responseFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

三、其他注意事项

1. JSP 页面编码设置

确保 JSP 页面头部声明了 UTF-8 编码:

jsp 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
2. 数据库连接编码

如果涉及数据库操作,在 JDBC URL 中指定字符集:

properties 复制代码
jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8
3. Ajax 请求编码

对于前端 Ajax 请求(如 jQuery),显式设置 contentType

javascript 复制代码
$.ajax({
    url: '/api/data',
    type: 'POST',
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8', // 明确设置编码
    data: { name: '张三' },
    success: function(response) {
        console.log(response);
    }
});

四、验证配置是否生效

  1. POST 请求测试

    提交表单后,观察后端是否能正确接收中文参数。

  2. GET 请求测试

    访问带中文参数的 URL,如 http://localhost:8080/user?name=张三,检查是否乱码。

  3. 响应内容测试

    返回 JSON 数据或视图页面,确认中文字符正常显示。


五、常见问题排查

  1. 乱码仅在部分场景出现

    • 检查是否遗漏了 GET 请求的 Tomcat 配置。
    • 确认前端请求头 Content-Type 是否携带 charset=UTF-8
  2. Spring Boot 项目配置

    application.properties 中添加:

    properties 复制代码
    server.servlet.encoding.force=true
    server.servlet.encoding.charset=UTF-8
  3. Tomcat 9+ 的兼容性

    新版 Tomcat 默认使用 UTF-8 编码,但仍需确保 URIEncoding 配置正确。


通过以上步骤,可以彻底解决 Spring MVC 中的中文乱码问题!

相关推荐
zzywxc78732 分钟前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
llwszx6 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
麦兜*9 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
CHENWENFEIc12 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试
高兴达13 小时前
RPC--Netty客户端实现
java·spring·rpc
要开心吖ZSH16 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring
考虑考虑17 小时前
Springboot3.4.x中的@Bean使用
spring boot·后端·spring
萧曵 丶20 小时前
Spring @TransactionalEventListener
java·数据库·spring·事务·transactional·异步
默默coding的程序猿21 小时前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
Kyrie_Li1 天前
(十五)Spring Test
java·后端·spring