使用JSON資料報錯和方法

來源:互聯網
上載者:User

標籤:避免   dia   exce   name   property   adapter   back   2.3   apt   

   今天自己寫了一個JSON的例子,可以一調用就出了問題,報下面這個異常:

Java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

上網上搜了一下才知道原來在使用JSON的使用的時候,不光要匯入JSON這個包,同時還要匯入另外三個包

總之,共需要下面的 jars,json-lib方可使用:

commons-beanutils-1.7.0.jar 

commons-lang-2.1.jar 

ezmorph-1.0.2.jar 

json-lib-1.1-jdk15.jar

 

但是又出現如下問題:

java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

匯入commons-logging-1.1.1.jar

 

又出現如下問題:

java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.JSONArray

匯入commons-collections 3.2

Ok

使用json常用到的包有以下六個。

[c-sharp] view plaincopy 
  1. commons-logging-1.0.4.jar  
  2. commons-lang-2.3.jar  
  3. commons-collections-3.2.jar  
  4. commons-beanutils-1.7.0.jar  
  5. json-lib-2.2.1-jdk15.jar  
  6. ezmorph-1.0.4.jar  

 

另外如果commons-collections-3.2.jar沒有或版本不對的話會出現

org/apache/commons/collections/map/ListOrderedMap這個異常。

 Spring MVC使用HttpClient返回json報406錯誤解決辦法1、確保匯入了jackson-core-asl-1.9.13.jar和jackson-mapper-asl-1.9.13.jar包
2、在spring的設定檔中加入<mvc:annotation-driven />這句,它提供了讀取jason的支援
3、使用springMVC的@ResponseBody註解@responsebody表示該方法的返回結果直接寫入HTTP response body中
一般在非同步擷取資料時使用,在使用@RequestMapping後,傳回值通常解析為跳轉路徑,加上@responsebody後返回結果不會被解析為跳轉路徑,而是直接寫入HTTP response body中。比如非同步擷取json資料,加上@responsebody後,會直接返回json資料。
4、在以上配置都正確的情況下,我的項目還是不能返回json串。報錯:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()。
今天終於在一個外文網站找到答案,是由於spring版本的問題引起的。我之前一直用的是3.0.0的版本。就是因為這個版本的問題。於是果斷去官網下載3.2版本的,一切正常運行,成功返回json資料。  spring mvc 避免IE執行AJAX時,返回JSON出現下載檔案

<!-- 避免IE執行AJAX時,返回JSON出現下載檔案 -->

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

 

<!-- 啟動spring MVC的註解功能,完成請求和註解POJO的映射 -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="messageConverters">

<list>

<ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉換器 -->

</list>

</property>

</bean>

 

去掉 <mvc:annotation-driven/>

 

使用JSON資料報錯和方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.