You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行《Camel in Action 2》Spring示例遇InvocationTargetException等问题求助

解决Camel in Action 2第2章Spring示例运行异常的方案

问题拆解

先说说第一个错误java.lang.ClassNotFoundException: org.apache.camel.spring.Main——这是因为你手动添加的camel-spring 2.25.3版本和项目依赖的Camel 3.20版本完全不兼容,Camel 3.x的组件结构和2.x差异很大,混合版本直接导致类找不到。

第二个错误找不到META-INF/spring/目录,是因为插件配置不对,加上依赖混乱,导致Spring没法识别配置文件路径。

具体修复步骤

1. 统一Camel依赖版本

删掉你手动加的camel-spring 2.25.3依赖,在POM的<dependencies>块里添加和项目Camel版本匹配的组件:
如果是Spring Boot项目,加这个:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-starter</artifactId>
    <version>3.20.9</version> <!-- 必须和项目的Camel版本一致 -->
</dependency>

如果是纯Spring项目(非Boot),加这个:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring</artifactId>
    <version>3.20.9</version>
</dependency>

2. 修正Maven插件配置

删掉你之前给camel-maven-plugin加的依赖块,重新配置插件,指定正确的Spring配置文件路径:

<plugin>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-maven-plugin</artifactId>
    <version>3.20.9</version> <!-- 版本必须和Camel主版本一致 -->
    <configuration>
        <!-- 这里填你项目里实际的Spring配置文件路径,比如默认示例里的META-INF/spring/camel-context.xml -->
        <springContextUri>META-INF/spring/camel-context.xml</springContextUri>
    </configuration>
</plugin>

如果你的配置文件直接放在src/main/resources根目录,就把路径改成camel-context.xml。

3. 确认项目配置文件存在

检查src/main/resources目录下有没有对应的配置文件,比如META-INF/spring/camel-context.xml——如果没有,说明你拉取代码的时候可能漏了,重新同步仓库代码就行。

4. 清理Maven缓存并重构

跑以下命令清理旧依赖,重新构建项目:

mvn clean install

然后用插件启动项目:

mvn camel:run

额外提示

  • Java 18和Camel 3.20可能存在兼容性问题,Camel 3.x对Java 11+支持,但更推荐用Java 11或17这类LTS版本,能少踩很多兼容性坑。
  • 所有Camel相关的依赖版本必须完全一致,绝对不能混合不同大版本的组件,不然各种类加载、依赖冲突问题会层出不穷。

内容的提问来源于stack exchange,提问作者R M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:40:06