【发布时间】:2016-09-10 16:39:26
【问题描述】:
我遇到了最新版本的 Camel 和 REST DSL 路由的问题。完整的代码可以在这里找到并运行:https://github.com/mikevoxcap/nvisia-catalog-camel-service。当我尝试运行路线时出现异常,根本原因如下:
Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.introspect.AnnotatedMember.getType()Lcom/fasterxml/jackson/databind/JavaType;
....
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:926)
at org.apache.camel.component.jackson.JacksonDataFormat.marshal(JacksonDataFormat.java:154)
at org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:69)
我的问题是,我应该针对哪个版本的 Jackson 不再收到此异常。
我引入了以下依赖项:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.boot.version>1.3.5.RELEASE</spring.boot.version>
<camel.version>2.17.1</camel.version>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-swagger-java</artifactId>
<version>${camel.version}</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
在对我收到的异常进行了一些搜索之后,有几个人在涉及版本冲突时遇到了这个问题。我注意到 camel-jackson 库引入了 jackson-module-jaxb-annotations 2.7.2,但其余的 jackson 库为 2.6.6。我尝试排除 jackson-module-jaxb-annotations 2.7.2,然后直接包含 2.6.6,但这仍然导致异常。
我的路线比较简单:
// Definition of the get catalog item endpoint
rest("/catalogItem").
// This is a GET method call for getting a catalog item by ID.
get("{id}").
// Description of what this method does
description("Retrieve a catalog item by ID").
// Define the output type that will be returned from this method
outType(CatalogItem.class)
// Define where the message is routed to as a URI. Here we use a
// Spring Bean and define the bean method to invoke. Note that Camel
// has converted the ID placeholder from the URL into a header
// entry.
.to("bean:catalogService?method=getCatalogItem(${header.id})");
我的对象被用作 JSON 类型也很简单:
public class CatalogItem {
private int id;
private double price;
private String catalogItemType;
private List<CatalogItemAttribute> attributes;
public class CatalogItemAttribute {
private String attributeName;
private String attributeValue;
【问题讨论】:
-
此错误
Caused by: java.lang.NoSuchMethodError表明您使用了错误的方法名称,或者您使用的依赖项版本不再实现此方法,或者您使用的库版本没有没有实施。 -
@JorgeCampos 是的,这就是我的问题所在。解析 POM 时,Jackson 注释依赖项是更高版本。我正在尝试确定是否应该排除核心 Jackson 库并将版本全面提高到 2.7.2,是否需要降级到低于 2.6.6 的版本。
-
所以我在 spring-boot-starter-web、spring-boot-starter-actuator、camel-jackson 和 camel-swagger- 中添加了 jackson-annotations、jackson-core 和 jackson-databind 的排除项java 依赖项。然后我用 2.7.2 专门定义了这三个库的依赖关系,代码现在可以工作了。经过一些研究,这似乎是 Boot 的问题,而不是 Camel。看起来像 1.4 的 Boot 更新到了 Jackson 的 2.7,所以我现在只需要给出这个答案。
标签: java json spring-boot jackson apache-camel