少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
           今天試著把SpringMVC與fastjson整合了下,經(jīng)測試也能解決json含中文亂碼的問題,特此分享之。我也是初用,詳細(xì)文檔請見官網(wǎng)
          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關(guān)鍵配置:
          <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 閱讀(5303) 評論(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)  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 高台县| 富宁县| 清新县| 海宁市| 锡林郭勒盟| 深水埗区| 霍城县| 梧州市| 遂平县| 榆树市| 博爱县| 久治县| 增城市| 岳阳县| 鄂托克前旗| 靖边县| 平度市| 潮安县| 印江| 鄄城县| 昭觉县| 精河县| 苏州市| 彭州市| 临漳县| 庐江县| 福建省| 盘山县| 彰化市| 永顺县| 定结县| 雷州市| 崇文区| 湟源县| 博爱县| 疏附县| 成都市| 定边县| 宕昌县| 海宁市| 砀山县|