springmvc字符 中文乱码问题

1.字符过滤器

输入中文测试,发现乱码

以前乱码问题通过过滤器解决 , 而SpringMVC给我们提供了一个过滤器 , 可以在web.xml中配置,修改了xml文件需要重启服务器。

springmvc未设置字符过滤器,获取的信息,在前端页面显示的中文都是中文乱码。

解决方法:在web.xml中设置字符过滤器

<filter>
<filter-name>encoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

但是发现 , 有些极端情况下.这个过滤器对get的支持不好 .

处理方法 :

修改tomcat配置文件 :

​ 文件位置:tomcat文件夹---conf---server.xml

​ 加入:URIEncoding="utf-8"

<Connectorport="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>

一般情况下,SpringMVC默认的乱码处理就已经能够很好的解决了!

乱码问题,需要平时多注意,在尽可能能设置编码的地方,都设置为统一编码 UTF-8

这种过滤器对大部分中文乱码都有用了,但是还有一种情况为json中文乱码

2.json乱码问题

中文变成????,这种情况常出现在:Controller类 使用了 @ResponseBody

​ @ResponseBody注解的作用是将controller的方法返回的对象 通过适当的转换器 转换为指定的格式之后,写入到response对象的body区(响应体中),通常用来返回JSON数据或者是XML。

​ 数据,需要注意的呢,在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,它的效果等同于通过response对象输出指定格式的数据。

这里还要着重强调一下,要通过@ResponseBody 注解 将返回的json字符串放入响应体中,然后在前台js才能拿到json字符串进行解析,如果不加,响应体中就没有放入json字符串,前台自然是拿不到数据的,希望大家别理解错。


解决方法:

2.1 第一种解决方法

@RequestMapping(value = "/xxx",produces = "application/json;charset=utf-8")

加入了produces = "application/json;charset=utf-8"

这种方法一般就可以解决问题了,真不行就下面这种

2.2 第二种解决方法

上一种方法比较麻烦,如果项目中有许多请求则每一个都要添加,可以通过Spring配置统一指定,这样就不用每次都去处理了

1.导入依赖:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>

2.在springmvc.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>

基本上问题就解决了

个人博客为:

MoYu's Github Blog

MoYu's Gitee Blog

springmvc字符 中文乱码问题的更多相关文章

  1. springmvc返回中文乱码问题

    关于springmvc的返回中文乱码的问题,网上可谓是清一色的一样,无外乎就两种,要么在局部类或这方法上解决,类似如下的代码: @GetMapping(value="/error/query ...

  2. SpringMVC RESTful中文乱码

    开发中常遇到各种中文乱码很少心烦,这里总结了各种中文乱码https://www.cnblogs.com/lwx521/p/9856186.html 下面以SpringMVC遇到的中文乱码为例详解 首先 ...

  3. SpringMVC页面中文乱码

    刚开始学习使用SpringMVC,完成配置之后开始编辑页面源码,添加了几个中文字符(index.jsp) <html> <body> <h2>hello world ...

  4. springmvc解决中文乱码问题

    1 第一种情况(get接收参数): 最近在用solr做一个搜索服务,发布给手机和pc等客户端调用,调用方式为:   http://www.ganbo.search/search?q="手机& ...

  5. springmvc数据处理-中文乱码

    首先解决中文乱码 通过mvc过滤器解决,在web.xml中配置 <filter> <filter-name>CharacterEncodingFilter</filter ...

  6. SpringMVC redirect中文乱码问题

    在使用"redirect:xxx.do?param=中文"时会出现乱码问题,解决方案如下: 使用model.addAttribute来替代直接拼接参数.如下: @RequestMa ...

  7. SpringMVC(二) —— 中文乱码处理

    Get的乱码处理 改tomcat中server.xml中的port=“8080”,加上一个 URIEncoding=”utf-8” 如下图: 2.Post乱码的处理 在web.xml文件中加入 < ...

  8. 解决SpringMVC+Thymeleaf中文乱码

    乱码效果截图 解决办法:在org.thymeleaf.templateresolver.ServletContextTemplateResolver和org.thymeleaf.spring5.vie ...

  9. springmvc StringHttpMessageConverter 中文乱码的几种解决办法(亲测)

    昨天,将一个原来使用JSR 311作为restful实现的测试系统改成了使用spring mvc,最后测试的时候发现输出的json字符串为乱码,从日志可以看出使用的是StringHttpMessage ...

随机推荐

  1. 一文弄懂使用Jmeter来进行性能测试

    该文章是基于上一次文章的 软件测试漫谈(web测试,自动化测试,Jmeter) 的续篇, 主要是详细讲解 Jmeter 的入门教程. 因为上次的文章只是简单地讲解了 Jmeter 的使用和一些概念,所 ...

  2. go免杀初探

    0x01 go免杀 由于各种av的限制,我们在后门上线或者权限持久化时很容易被杀软查杀,容易引起目标的警觉同时暴露了自己的ip.尤其是对于windows目标,一个免杀的后门极为关键,如果后门文件落不了 ...

  3. wordpress 主题安装 您点击的链接已过期 nginx 出现413 Request Entity Too Large

    1 nginx 出现413 Request Entity Too Large 问题是限制上传大小,解决: 1.打开nginx配置文件 nginx.conf, 路径一般是:/etc/nginx/ngin ...

  4. 牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解

    题意1.1: 求\(\sum_{i=1}^n Fib^m\mod 1e9+9\),\(n\in[1, 1e9], m\in[1, 1e4]\) 思路1.1 我们首先需要知道斐波那契数列的通项是:\(F ...

  5. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  6. ES6 Map vs ES5 Object

    ES6 Map vs ES5 Object Map vs Object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  7. vue-parent-child-lifecycle-order

    vue-parent-child-lifecycle-order vue parent child lifecycle order live demo https://99kyx.csb.app/ h ...

  8. Array in Depth

    Array in Depth Array.concat() & Array.push() https://developer.mozilla.org/en-US/docs/Web/JavaSc ...

  9. Flutter ColorFiltered 将ColorFilter应用于其子级。

    ColorFiltered ColorFilter BlendMode Example <summary>main.dart</summary> import 'package ...

  10. NGK引入反量子加密系统来应对量子计算攻击

    当前,区块链和分布式账本技术已有了长足发展并广泛应用与多种场景中,原因在于其提供透明性,冗余性和问责性的能力.就区块链而言,此类特征是通过公钥加密和哈希函数提供的.但是,随着量子计算机技术的发展和量子 ...