我开始学习camel,我正试图用java spring Boot 做一个简单的代码。下面是我代码:
package camel;
import org.apache.camel.CamelContext;
import org.apache.camel.Configuration;
import org.apache.camel.Consume;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@Configuration
public class CamelConfig extends RouteBuilder {
@Autowired
private CamelContext camelContext;
@Override
public void configure() throws Exception {
from("direct:myRoute")
// from("timer:myTimer?period=5000")
.setBody().constant("Hello, world!")
.to("log:output");
}
@Bean
public ProducerTemplate producerTemplate() {
return camelContext.createProducerTemplate();
}
}
控制器:
@Autowired
private ProducerTemplate producerTemplate;
@GetMapping("/hello")
public String sayHello() {
producerTemplate.start();
producerTemplate.sendBody("direct:myRoute", "Hello from REST API!");
return "Message sent to Camel route";
}
pom.xml
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>3.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>3.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-swagger-java-starter</artifactId>
<version>3.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.21.0</version>
</dependency>
当我尝试发送get请求时,我收到状态为500的失败,并出现以下错误:终结点上没有可用的使用者:direct://myRoute.#21453;的交流,有人知道为什么吗?
3条答案
按热度按时间u7up0aaq1#
问题可能是您混合了Camel和Spring注解。尝试使用来自spring
import org.springframework.context.annotation.Configuration
的@Configuration
,而不是import org.apache.camel.Configuration
。一般来说,我建议将Spring配置与Camel路由配置分开。如果你使用的是springboot和
camel-spring-boot-starter
,那么你可以跳过Spring会为你做的。让你的类像这样:
还要仔细查看您正在使用的软件包版本
对于camel 3(3.20)-使用springboot 2(2.7.14)。如果你想换到camel 4.0.0,可以切换到springboot 3和Java 17
6l7fqoea2#
我又补充了一句:
@Configuration @ComponentScan("camel")
控制器,现在它的工作!tnx!
a9wyjsp73#
我把它改成: