SpringMVC整合fastjson、easyui 乱码问题

一、框架版本
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添加均可(解决问题)








posted @ 2016-04-11 17:28    阅读(1745)  评论(0编辑  收藏  举报