少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
 今天试着把SpringMVC与fastjson整合了下,经测试也能解决json含中文乱码的问题,特此分享之。我也是初用,详细文档请见官网
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(); 
    } 




SpringMVC关键配置:
<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> 


http://xyly624.blog.51cto.com/842520/896704
posted on 2013-01-12 23:56 abin 阅读(5299) 评论(1)  编辑  收藏 所属分类: JSON

Feedback

# re: SpringMVC与fastjson整合并同时解决中文乱码问题[未登录] 2013-03-21 00:38 test
2013-03-21 00:33:43,718 ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 102 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: