java - 其余模板的 Spring JsonProcessingExceptin NoClassDefFoundError

标签 java json spring resttemplate

我正在使用 jackson 解析器,它在请求获取时与我的其余模板一起使用,但是当我尝试发布帖子时,它给了我以下错误:

web.util.NestedServletException: 
Handler processing failed; 
nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

我不知道出了什么问题。我正在尝试发送一个发布请求,其正文应该是一个对象:

@RequestMapping(value = "dogCreated", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public String createDog(@ModelAttribute Dog dog, BindingResult result, Model model){
RestTemplate rest = new RestTemplate();
//      List< HttpMessageConverter<?> > messageConverters = new ArrayList< HttpMessageConverter<?> >();   
//      // empty list created
//
//      messageConverters.add( new MappingJackson2HttpMessageConverter() );      
//      messageConverters.add( new StringHttpMessageConverter() );

        rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());/*This is where the error occurs*/
        rest.getMessageConverters().add(new StringHttpMessageConverter());

        ObjectMapper mapper = new ObjectMapper();
        TypeFactory typeFactory = mapper.getTypeFactory();

        String response = rest.postForObject("https://rest-service.herokuapp.com/createDog", dog, String.class);
        System.out.println(response);

我已将 Jackson 依赖项包含在我的 pom.xml 中:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

谁能告诉我哪里出错了?更具体地说,它说错误发生在这一行:

rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

任何帮助将不胜感激。

这是完整的堆栈跟踪

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
    org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1303)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:977)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
    com.dogs.controller.DogController.createDog(DogController.java:42)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
    com.dogs.controller.DogController.createDog(DogController.java:42)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

最佳答案

您遇到的问题是因为您使用的是最新的 spring 不支持的非常旧的依赖项:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

由于上述库发生了一点变化,您需要删除该依赖项并添加新的依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.4</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.4</version>
</dependency>

更改它并查看结果。如果有效,请将其作为答案。

关于java - 其余模板的 Spring JsonProcessingExceptin NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487290/

相关文章:

unit-testing - 使用 Spring 配置测试 Camel 的问题

Java for循环跳过运行两次

java - 使用 PHP Soap 服务器和 Java 客户端的 Web 服务

java - MySQLIntegrityConstraintViolationException : Duplicate entry '1' for key 'PRIMARY'

php - 通过来自 MySql 数据库的 JSON 在 WP_User_Query 中放置数组

java - 在 JUnit 中获取 java.lang.ClassNotFoundException : javax. servlet.ServletContext

java - 从方法返回值而不创建对象

Json 提供的类型生成不适用于 "with"

node.js - 读取动态更新文件夹中包含的所有 JSON 文件

java - Sprint TestRestTemplate 返回 Id null