一、框架版本

SpringMVC:3.1.1.RELEASE

fastjson:1.2.7
easyui :1.4.5

二、乱码现象
   Action中使用@ResponseBody返回Json数据
  1、Action返回的数据正常,无乱码现象

  2、使用浏览器的开发者模式查看返回值,发现乱码
可以确定乱码问题出现在数据返回到浏览器的过程中


三、解决过程
1、最常规的方法,添加message-converters,添加后如json-lib库可以解决乱码问题,但是fastjson无法解决(未解决)
1
2
3
4
5
6
7
<mvc:annotation-driven>
         <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
              <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
            </bean>
           </mvc:message-converters>
     </mvc:annotation-driven>

2、实现AbstractHttpMessageConverter抽象类(未解决)

http://xyly624.blog.51cto.com/842520/896704

该方法解决了返回乱码问题,但是easyui无法显示数据(有数据但是显示为空,原因不明)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class MappingFastJsonHttpMessageConverter extends
        AbstractHttpMessageConverter<Object> {
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
  
    private SerializerFeature[] serializerFeature;
  
    public SerializerFeature[] getSerializerFeature() {
        return serializerFeature;
    }
  
    public void setSerializerFeature(SerializerFeature[] serializerFeature) {
        this.serializerFeature = serializerFeature;
    }
  
    public MappingFastJsonHttpMessageConverter() {
        super(new MediaType("application", "json", DEFAULT_CHARSET));
    }
  
    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        return true;
    }
  
    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return true;
    }
  
    @Override
    protected boolean supports(Class<?> clazz) {
        throw new UnsupportedOperationException();
    }
  
    @Override
    protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
    throws IOException, HttpMessageNotReadableException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i;
        while ((i = inputMessage.getBody().read()) != -1) {
            baos.write(i);
        }
        return JSON.parseArray(baos.toString(), clazz);
    }
  
    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {
        String jsonString = JSON.toJSONString(o, serializerFeature);
        OutputStream out = outputMessage.getBody();
        out.write(jsonString.getBytes(DEFAULT_CHARSET));
        out.flush();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">       
        <!-- fastjosn spring support -->
        <bean id="jsonConverter" class="com.alibaba.fastjson.spring.support.MappingFastJsonHttpMessageConverter">
            <property name="supportedMediaTypes" value="application/json" />
            <property name="serializerFeature">
                <list>
                    <value>WriteMapNullValue</value>
                    <value>QuoteFieldNames</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

3、@RequestMapping添加produces = "text/html;charset=UTF-8",在Controller或Action添加均可(解决问题)

SpringMVC整合fastjson、easyui 乱码问题的更多相关文章

  1. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. SpringMVC整合FastJson:用"最快的json转换工具"替换SpringMVC的默认json转换

    2017年11月23日 09:18:03 阅读数:306 一.环境说明 Windows 10 1709 Spring 4.3.12.RELEASE FastJson 1.2.40 IDEA 2017. ...

  3. 解决springmvc+mybatis+mysql中文乱码问题【转】

    这篇文章主要介绍了解决java中springmvc+mybatis+mysql中文乱码问题的相关资料,需要的朋友可以参考下 近日使用ajax请求springmvc后台查询mysql数据库,页面显示中文 ...

  4. springMVC整合jedis+redis,以注解形式使用

    前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和springmvc整合到一起. 和memcached一样,redis也有java专用的客户端,官网推荐 ...

  5. springMVC整合jedis+redis

    http://www.cnblogs.com/zhengbn/p/4140549.html 前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和sprin ...

  6. SpringMVC整合fastjson-1.1.41

    以前用fastjson也只是硬编码,就好像这篇博文写的http://blog.csdn.net/jadyer/article/details/24395015 昨天心血来潮突然想和SpringMVC整 ...

  7. springMVC注解方式+easyUI+MYSQL配置实例

    刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...

  8. JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别

    1. 学习计划   第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...

  9. springmvc 整合微信

    springmvc 整合微信 方式一: ① 配置验证 @RequestMapping(value = "/into", method = RequestMethod.GET, pr ...

随机推荐

  1. asp基础

    0.1在浏览器中通过查看源代码的方式是无法看到 ASP 源代码的,你只能看到由 ASP 文件输出的结果,而那些只是纯粹的 HTML 而已.这是因为,在结果被送回浏览器前,脚本已经在服务器上执行了. 0 ...

  2. Condition接口

    <Java并发编程艺术>读书笔记 Condition介绍 任意一个Java对象,都拥有一组监视器方法(定义在java.lang.Object中),主要包括wait().wait(long ...

  3. linux shell 正则表达式(BREs,EREs,PREs)的比较

    原文 :   linux shell 正则表达式(BREs,EREs,PREs)差异比较 在使用 linux shell的实用程序,如awk,grep,sed等,正则表达式必不可少,他们的区别是什么 ...

  4. 深度扫盲JavaScript的模块化(AMD , CMD , CommonJs 和 ES6)

    原文地址 https://blog.csdn.net/haochangdi123/article/details/80408874 一.commonJS 1.内存情况 对于基本数据类型,属于复制.即会 ...

  5. 第二章:监控属性(Observables)

    关于Knockout的3个重要概念(Observables,DependentObservables,ObservableArray),本人无法准确表达它的准确含义,所以暂定翻译为(监控属性.依赖监控 ...

  6. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  7. cvc-complex-type.2.4.d: 发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素。

      Eclipse上开发Android的时候,新建的项目提示: [2016-01-13 14:07:56 - android SDK] Error when loading the SDK: Erro ...

  8. Django学习笔记-2018.12.08

    在Python的正则表达式中,有一个参数为re.S.它表示“.”(不包含外侧双引号,下同)的作用扩展到整个字符串,包括“\n”.看如下代码: import re a = '''asdfhellopas ...

  9. 可随意交换位置的gridview

    自定义gridview import android.app.Activity; import android.content.Context; import android.graphics.Poi ...

  10. Android之 解析XML文件(1)—— Pull解析

    (以下文章基本照抄<第一行代码>) 解析XML文件有很多方法,这里主要讲Pull解析和SAX解析.这篇文章主要是讲Pull解析. 一.Pull解析参考代码 先上代码: private vo ...