使用Spring4时, 运行时出现找不到MappingJacksonHttpMessageConverter的情况
启动项目报错:
[org.springframework.web.context.ContextLoader]Context initialization failed org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0' ... ... Caused by: org.springframework.beans.factory.CannotLoadBeanClassException:
Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] for bean with name 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverter#2aa148c0' ... ... Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter ... ...
原因是Spring 3.x 和4.X处理JSON的一个类不一样,而这个东西又配置在xml文件中,所以编译时又无法发现
spring3.x是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
spring4.x是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
1. 查找替换所有XML中名字
2. 同时加入最新的FastJackson依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.5</version> </dependency>
问题解决!
持续学习、持续收获才能带来持续的满足和快乐!