轉載:Spring整合JSON報錯:java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException

假裝鎮定發表於2018-09-06

轉載:https://blog.csdn.net/jiaotuwoaini/article/details/52366057

先看異常資訊:

  1. SEVERE: StandardWrapper.Throwable
  2. java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonProcessingException
  3. at java.lang.Class.getDeclaredConstructors0(Native Method)
  4. at java.lang.Class.privateGetDeclaredConstructors(Class.java:2493)
  5. at java.lang.Class.getDeclaredConstructors(Class.java:1901)
  6. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:234)
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1040)
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1013)
  9. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
  10. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
  11. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
  12. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
  13. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
  14. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
  15. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
  16. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
  17. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
  18. at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
  19. at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
  20. at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
  21. at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
  22. at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
  23. at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
  24. at javax.servlet.GenericServlet.init(GenericServlet.java:158)
  25. at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
  26. at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
  27. at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
  28. at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4909)
  29. at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
  30. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  31. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
  32. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
  33. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
  34. at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1069)
  35. at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1719)
  36. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  37. at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  38. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  39. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  40. at java.lang.Thread.run(Thread.java:744)
  41. Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException
  42. at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
  43. at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
  44. ... 38 more

看我們的pom.xml配置:

  1. <properties>
  2. <!-- spring版本號 -->
  3. <spring.version>4.0.2.RELEASE</spring.version>
  4. <!-- mybatis版本號 -->
  5. <mybatis.version>3.2.6</mybatis.version>
  6. <!-- log4j日誌檔案管理包版本 -->
  7. <slf4j.version>1.7.7</slf4j.version>
  8. <log4j.version>1.2.17</log4j.version>
  9. <jackson.version>2.8.1</jackson.version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.11</version>
  16. <!-- 表示開發的時候引入,釋出的時候不會載入此包 -->
  17. <scope>test</scope>
  18. </dependency>
  19. <!-- spring核心包 -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-core</artifactId>
  23. <version>${spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-web</artifactId>
  28. <version>${spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-oxm</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-tx</artifactId>
  38. <version>${spring.version}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-jdbc</artifactId>
  43. <version>${spring.version}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework</groupId>
  47. <artifactId>spring-webmvc</artifactId>
  48. <version>${spring.version}</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework</groupId>
  52. <artifactId>spring-aop</artifactId>
  53. <version>${spring.version}</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-context-support</artifactId>
  58. <version>${spring.version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.springframework</groupId>
  62. <artifactId>spring-test</artifactId>
  63. <version>${spring.version}</version>
  64. </dependency>
  65. <!-- mybatis核心包 -->
  66. <dependency>
  67. <groupId>org.mybatis</groupId>
  68. <artifactId>mybatis</artifactId>
  69. <version>${mybatis.version}</version>
  70. </dependency>
  71. <!-- mybatis/spring包 -->
  72. <dependency>
  73. <groupId>org.mybatis</groupId>
  74. <artifactId>mybatis-spring</artifactId>
  75. <version>1.2.2</version>
  76. </dependency>
  77. <!-- 匯入java ee jar 包 -->
  78. <dependency>
  79. <groupId>javax</groupId>
  80. <artifactId>javaee-api</artifactId>
  81. <version>7.0</version>
  82. </dependency>
  83. <!-- 匯入Mysql資料庫連結jar包 -->
  84. <dependency>
  85. <groupId>mysql</groupId>
  86. <artifactId>mysql-connector-java</artifactId>
  87. <version>5.1.30</version>
  88. </dependency>
  89. <!-- 匯入dbcp的jar包,用來在applicationContext.xml中配置資料庫 -->
  90. <dependency>
  91. <groupId>commons-dbcp</groupId>
  92. <artifactId>commons-dbcp</artifactId>
  93. <version>1.2.2</version>
  94. </dependency>
  95. <!-- JSTL標籤類 -->
  96. <dependency>
  97. <groupId>jstl</groupId>
  98. <artifactId>jstl</artifactId>
  99. <version>1.2</version>
  100. </dependency>
  101. <!-- 日誌檔案管理包 -->
  102. <!-- log start -->
  103. <dependency>
  104. <groupId>log4j</groupId>
  105. <artifactId>log4j</artifactId>
  106. <version>${log4j.version}</version>
  107. </dependency>
  108. <!-- 格式化物件,方便輸出日誌 -->
  109. <dependency>
  110. <groupId>com.alibaba</groupId>
  111. <artifactId>fastjson</artifactId>
  112. <version>1.1.41</version>
  113. </dependency>
  114. <dependency>
  115. <groupId>org.slf4j</groupId>
  116. <artifactId>slf4j-api</artifactId>
  117. <version>${slf4j.version}</version>
  118. </dependency>
  119. <dependency>
  120. <groupId>org.slf4j</groupId>
  121. <artifactId>slf4j-log4j12</artifactId>
  122. <version>${slf4j.version}</version>
  123. </dependency>
  124. <!-- log end -->
  125. <!-- 映入JSON -->
  126. <span style="color:#ff0000;"> <dependency>
  127. <groupId>com.fasterxml.jackson.core</groupId>
  128. <artifactId>jackson-databind</artifactId>
  129. <version>${jackson.version}</version>
  130. </dependency>
  131. <dependency>
  132. <groupId>com.fasterxml.jackson.core</groupId>
  133. <artifactId>jackson-annotations</artifactId>
  134. <version>${jackson.version}</version>
  135. </dependency>
  136. <dependency>
  137. <groupId>com.fasterxml.jackson.core</groupId>
  138. <artifactId>jackson-core</artifactId>
  139. <version>${jackson.version}</version>
  140. </dependency></span>
  141. <!-- 上傳元件包 -->
  142. <dependency>
  143. <groupId>commons-fileupload</groupId>
  144. <artifactId>commons-fileupload</artifactId>
  145. <version>1.3.1</version>
  146. </dependency>
  147. <dependency>
  148. <groupId>commons-io</groupId>
  149. <artifactId>commons-io</artifactId>
  150. <version>2.4</version>
  151. </dependency>
  152. <dependency>
  153. <groupId>commons-codec</groupId>
  154. <artifactId>commons-codec</artifactId>
  155. <version>1.9</version>
  156. </dependency>
  157. <dependency>
  158. <groupId>org.apache.httpcomponents</groupId>
  159. <artifactId>httpclient</artifactId>
  160. <version>4.5.2</version>
  161. </dependency>
  162. </dependencies>

可以看到紅色的jackson已經被配置進去了,為什麼還報錯?

經過排查發現Spring的配置是從網上拷貝的,版本比較老,其中關於json的配置是:

  1. <!--避免IE執行AJAX時,返回JSON出現下載檔案 -->
  2. <bean id="mappingJacksonHttpMessageConverter"
  3. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  4. <property name="supportedMediaTypes">
  5. <list>
  6. <value>text/html;charset=UTF-8</value>
  7. </list>
  8. </property>
  9. </bean>
如果將Spring的版本提高到最新的版本(4.3.2.RELEASE),這裡其實是會報錯的。

原來是實現類變化了,現在更改為:

  1. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  2. <property name="supportedMediaTypes">
  3. <list>
  4. <value>text/html;charset=UTF-8</value>
  5. </list>
  6. </property>
  7. </bean>
注意中間的 2 。



相關文章