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

Apache Camel 3.1升3.2后自定义组件触发NoSuchEndpointException问题

问题排查与解决方案

核心原因

Apache Camel 3.2 版本调整了自定义组件/端点的自动注册逻辑,移除了3.1版本中对单独@UriEndpoint标注类的默认Component实例生成逻辑,同时强化了组件scheme与注册bean名的匹配校验,这是导致升级后出现端点找不到异常的核心原因。

排查步骤

  • 第一步:验证当前Camel上下文已注册的组件列表,调用camelContext.getComponentNames()打印输出,确认my-rest-client是否在列表中。如果不在,说明组件未完成注册。
  • 第二步:检查Spring上下文的Bean定义,确认是否存在与schememy-rest-client同名的Camel Component类型Bean。3.2版本不再允许仅注册Endpoint实例就完成组件映射,必须配套对应的Component实现。
  • 第三步:开启Camel DEBUG级日志,搜索Loading component关键词,查看my-rest-client组件的加载过程是否存在异常。

修复方案

方案一:补全自定义Component实现(推荐)

  1. 新增自定义Component类,Bean名称与scheme完全一致
import org.apache.camel.support.DefaultComponent;

@Component("my-rest-client")
public class MyRestClientComponent extends DefaultComponent {
    @Override
    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        MYClientEndpoint endpoint = new MYClientEndpoint(uri, this);
        // 注入Spring托管的Producer实例
        endpoint.setMYClientProducer(getCamelContext().getRegistry().lookupByNameAndType("MYClientProducer", MYClientProducer.class));
        setProperties(endpoint, parameters);
        return endpoint;
    }
}
  1. 调整现有MYClientEndpoint代码,移除硬编码的getEndpointUri方法,新增构造方法
@UriEndpoint(scheme = "my-rest-client",title = "my-rest-client",syntax = "my-rest-client")
public class MYClientEndpoint extends DefaultEndpoint {
    private MYClientProducer MYClientProducer;

    public MYClientEndpoint(String uri, MyRestClientComponent component) {
        super(uri, component);
    }

    @Override
    public Producer createProducer() {
        return MYClientProducer;
    }

    @Override
    public Consumer createConsumer(Processor processor) {
        throw new UnsupportedOperationException("Operation not supported");
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    // 新增setter供Component注入Producer
    public void setMYClientProducer(MYClientProducer MYClientProducer) {
        this.MYClientProducer = MYClientProducer;
    }
}

方案二:手动注册端点实例(快速适配)

如果暂时不想改动原有结构,可以直接将已托管到Spring的端点实例手动注册到Camel上下文:

@Autowired
private MYClientEndpoint myClientEndpoint;

@Autowired
private CamelContext camelContext;

@PostConstruct
public void registerCustomEndpoint() throws Exception {
    camelContext.addEndpoint("my-rest-client", myClientEndpoint);
}

额外验证点

  • 如果使用Spring Boot环境,确认配置文件中未禁用Camel的组件扫描:camel.springboot.name-scan-enabled=true
  • 移除原代码中错误的@Component("MYClientEndpoint")注解,Endpoint不需要作为独立Bean托管到Spring,由Component负责创建实例即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:45:03