Spring 5.0.4&杰克逊2在创业时抛出了异常

时间:2018-05-06 10:05:27

标签: java spring rest jackson2

我正在尝试制作简单的REST服务,但遇到了2个问题。 1. REST控制器的任何方法都返回406 http错误;

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


> 06-May-2018 15:36:45.477 WARNING [http-nio-25565-exec-3]
> org.springframework.context.support.AbstractApplicationContext.refresh
> Exception encountered during context initialization - cancelling
> refresh attempt:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'requestMappingHandlerAdapter' defined in
> class path resource
> [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:
> Bean instantiation via factory method failed; nested exception is
> org.springframework.beans.BeanInstantiationException: Failed to
> instantiate
> [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]:
> Factory method 'requestMappingHandlerAdapter' threw exception; nested
> exception is java.lang.NoClassDefFoundError:
> com/fasterxml/jackson/databind/exc/InvalidDefinitionException

所以谷歌告诉我,这个问题可能是缺少Jackson2的依赖。   但添加此依赖项会导致此错误,添加较低版本会导致相同的输出或406

{{1}}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

在Spring 5或更高版本中, BeanInstantiationException 类负责引发 java.lang.NoClassDefFoundError:com / fasterxml / jackson / databind / exc / InvalidDefinitionException 作为嵌套异常

从上述异常可以明显看出,此异常的根本原因是缺少类 InvalidDefinitionException 。从2.9.0版开始,此类已在jackson databind API中引入。

因此,要解决此问题,只需添加2.9.0或更高版本的jackson databind依赖项

<依赖关系>
     com.fasterxml.jackson.core
     jackson-databind
     2.9.0

谢谢!!