【SpringMVC】一次处理项目中文乱码的经历
2024-08-26 08:45:42
一次处理项目中文乱码的经历
背景
今天把旧服务器上的项目转移到新服务器上,结果返回的json中的中文乱码了,觉得很奇怪,因为新服务器和旧服务器都是TX云,也不会有太大区别呀,于是乎开始了为期半天的蛋疼之旅。
项目使用的是SpringMVC+MySQL+Mybatis,于是从各个方面查看Bug到底躲在哪,以下是我搜集到的和使用到的方法:
在web.xml中加入编码过滤器
修改web.xml,加入如下filter:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
连接数据库时加入字符编码
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
RequestMapping注解中加入produces字段
@RequestMapping(value = "/test", produces="application/json;charset=UTF-8")
SpringMVC配置文件spring-mvc.ml中加入消息转换器
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
SpringMVC配置文件spring-mvc.ml中加入StringHttpMessageConverter
<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>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
The End
其实上面这些在项目中都已经使用了,最后我也是实在是找不到还有哪些没做的地方,于是开始怀疑是不是我数据库有问题,用了以前的数据库发现果然不乱码,一查数据库果然,数据本身存进去的时候就乱码了。因为是新开的机器,没有中文语言,我又在上面的MySQL执行了sql文件,所以···浪费了好长时间!!!
想想时间也浪费了,不如好好整理一下吧,也许对你们有用呢_
另外发现AnnotationMethodHandlerAdapter和MappingJacksonHttpMessageConverter都是已经deprecated的类,也许对应的技术也应该更新换代了。
最新文章
- 【译】Unity3D Shader 新手教程(4/6) &mdash;&mdash; 卡通shader(入门版)
- 2013-08-12【随笔2】-Roy
- 什么是REST?
- ligerui_ligerTree_006_ligerui事件支持
- VMware Tools安装
- 在 virtualbox 的 centos7 虚拟机中安装增强工具
- Kernel panic - not syncing: Attempted to kill init
- WebService CXF调试常见报错及解决方案
- 1.0 Python 学习网站
- Linux 新手应该知道的一些求助命令
- 图解java中的bytebuffer
- [Swift]LeetCode384. 打乱数组 | Shuffle an Array
- python-函数参数
- jarvis OJ WEB题目writeup
- 46-2016 蓝桥杯 java B 组
- 关于excel中的查找
- stylus 知识点
- MySQL主从.md
- mxonline实战9,我要学习功能块,机构详情展示,收藏功能
- 【SpringBoot+Mybatis+thymeleaf报错】Error resolving template ";XXX";, template might not exist or might not be accessible by any of the configured
热门文章
- SpringBoot Caused by: java.lang.NoClassDefFoundError: org/apache/tomcat/util/descriptor/tld/TldParser
- 2016-2017 2 20155335《java程序设计》第四周总结
- 基于springmvc静态文件资源配置问题
- 将资源文件夹中的文件通过流的方式写入到应用的File文件夹中
- 启动Eclipse时,弹出failed to load the jni shared library
- css控制文字换行
- 第一章:读取文件一行IO::File
- 一个简单爬免费代理IP的脚本
- python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)【转】
- 定制LFS镜像及安装过程