I have written sample code for calling rest api using apache camel. Which is working correctly in standalone but the same code I have used to create OSGI bundle and deploy it into the karaf container that the bundle is created sucessfully but i am getting the error such as "No component found with scheme http" when i try to call it.
Can you help me to resolve this issue?
Here's the code :
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();
Error below :
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
Add following snipplet to your 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>
If you use Camel in a OSGI/Karaf/ServiceMix/JBoss FUSE ESB Environment you have to add the bundle via Karaf console with
features:install camel-http
Find more information about installing camel for Karaf, have a look at http://camel.apache.org/karaf
If you create the camel context in OSGi, you need to create OsgiDefaultCamelContext instead of DefaultCamelContext, and you need to pass the bundle context as the construction parameter.
If you are using Blueprint or Spring, it could be much easy for you by look up the camel context from the Application context then create a new camel context yourself.
Initializing the beans solved the issue. My app was using Camel with Spring Boot and DefaultCamelContext had "scheme" value as Null as the httpComponent was not set.
Hence, no component found with scheme: https
Initializing the bean on startup the scheme was set as expected.
import org.springframework.context.annotation.Bean;
@Bean({"http","https"})
HttpComponent httpComponent() {
return new HttpComponent();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With