在Apache Camel中出现“找不到使用http方案的组件”错误

14

我编写了一份使用Apache Camel调用REST API的示例代码。这份代码在独立环境下可以正常运行,但当我将同样的代码用于创建OSGI bundle并将其部署到Karaf容器中时,尽管bundle已经成功创建,但是当我尝试调用它时出现了"No component found with scheme http"等错误信息。

请问您能帮助我解决这个问题吗?

这是我的代码:

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("direct:start")
                .setHeader(Exchange.HTTP_METHOD,simple("GET"))
                .to("http://10.10.10.10:8080/RestfulDemo/rest/get");
            }
        });

        context.start();

        ProducerTemplate template = context.createProducerTemplate();
        String headerValue = "application/xml";

        Map<String, Object> headers = new HashMap<String,Object>();
        headers.put("Content-Type", headerValue);

        Object result = template.requestBodyAndHeaders("direct:start", null, headers, String.class);
        Exchange exchange = new DefaultExchange(context); 
        String response = ExchangeHelper.convertToType(exchange, String.class, result); 
        System.out.println("Response : "+response);
        context.stop();

以下是错误信息:

org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: http://10.10.10.10:8080/RestfulDemo/rest/get due to: No component found with scheme: http

添加整个错误堆栈跟踪信息会有所帮助。 - Martín Schonaker
5个回答

20

请将以下代码片段添加到您的pom.xml文件中:

<dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-http</artifactId>
     <version>x.x.x</version>
     <!-- use the same version as your Camel core version -->
 </dependency>

如果您在OSGI/Karaf/ServiceMix/JBoss FUSE ESB环境中使用Camel,则必须通过Karaf控制台添加bundle。

features:install camel-http

查找有关在Karaf上安装Camel的更多信息,请查看http://camel.apache.org/karaf


2
我已经对两个版本使用了相同的版本,但仍然遇到了相同的问题。 - Azhaguvel A
@AzhaguvelA 你使用OSGI环境吗?我在我的回答中添加了如何添加camel-http功能。 - Peter Keller
如果已经使用“list | grep camel-http”命令安装了“camel-http”并且状态为“Active”,则无需再次安装它。 - Peter Keller
camel-http 出现在 osgi:list 而不是 features:list 中。这是正确的安装方式还是需要将其安装在 features 组下? - Azhaguvel A
如果在 features:list 下找不到它,那么请参考我的答案尝试使用 features:install camel-http。如果您使用 karaf,请查看 http://camel.apache.org/karaf 以获取更多信息。 - Peter Keller
显示剩余2条评论

4
如果您在OSGi中创建骆驼上下文,则需要创建OsgiDefaultCamelContext而不是DefaultCamelContext,并将BundleContext作为构造参数传递。
如果您使用Blueprint或Spring,则可以通过从应用程序上下文中查找骆驼上下文,然后自己创建一个新的骆驼上下文,这样会更加容易。

1

初始化bean解决了问题。 我的应用使用了Spring Boot和Camel,DefaultCamelContext的“scheme”值为Null,因为httpComponent未设置。

因此,找不到方案为https的组件

在启动时初始化bean,方案被设置为预期值。

import org.springframework.context.annotation.Bean;

@Bean({"http","https"})
HttpComponent httpComponent() {
    return new HttpComponent();
}   

考虑指定这段代码片段应放置的位置,以及是什么导致了您得出这个结论。 - Evgeny Mamaev

0

不仅将该条目添加到pom中,还要像这样将HTTPComponent对象添加到您的camel上下文中...

    HttpComponent httpComponent = new HttpComponent();
    context.addComponent("http", httpComponent);

0
请在您的pom或build.gradle中添加apache camel-http依赖项,这将解决您的错误。
例如:implementation group: 'org.apache.camel', name: 'camel-http', version: '3.1.0'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接