Apache Camel with Spring Boot
Spring Boot auto-configures a CamelContext when camel-spring-boot-starter is on the classpath. Routes are discovered automatically from RouteBuilder beans: no XML required.
Contents
Dependencies
Add the Camel Spring Boot BOM for version management, then include the starter plus any component starters you need.
<!-- Camel Spring Boot BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>4.6.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Core Camel + Spring Boot auto-config -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<!-- Add component starters as needed -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-kafka-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
</dependency>
</dependencies>
CamelContextcontext.start()Defining Routes with RouteBuilder
Annotate a class with @ComponentRouteBuilderCamelContext
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class OrderRoute extends RouteBuilder {
@Override
public void configure() {
// Poll the inbox folder every 5 seconds
from("file:data/inbox?delay=5000&delete=true")
.routeId("file-to-log") // optional: give the route a name
.log("Received file: ${header.CamelFileName}")
.to("direct:processOrder");
from("direct:processOrder")
.log("Processing order: ${body}");
}
}
RouteBuilderrouteId()Camel Properties in application.properties
Camel Spring Boot exposes camel.springboot.*{{placeholders}}application.properties
# Give the CamelContext a friendly name (visible in JMX and logs)
camel.springboot.name=MyIntegrationApp
# Keep the Spring Boot application alive while routes are running
camel.springboot.main-run-controller=true
# Custom properties used inside routes
app.inbox.dir=data/inbox
app.outbox.dir=data/outbox
app.poll.delay=5000
@Component
public class FileRoute extends RouteBuilder {
@Override
public void configure() {
// {{placeholders}} resolve from application.properties at startup
from("file:{{app.inbox.dir}}?delay={{app.poll.delay}}&delete=true")
.log("File received: ${header.CamelFileName}")
.to("file:{{app.outbox.dir}}");
}
}
Injecting Spring Beans into Routes
Spring beans are available inside RouteBuilder@Autowiredbean()
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class OrderRoute extends RouteBuilder {
@Autowired
private OrderService orderService; // a regular Spring @Service
@Override
public void configure() {
from("timer:orders?period=3000")
// Call a Spring bean method directly
.bean(orderService, "generateOrder")
.log("Generated: ${body}")
// Or reference a bean by Spring bean name
.to("bean:orderService?method=saveOrder");
}
}
import org.springframework.stereotype.Service;
@Service
public class OrderService {
public String generateOrder() {
return "ORDER-" + System.currentTimeMillis();
}
public void saveOrder(String order) {
System.out.println("Saved: " + order);
}
}
bean()REST DSL
The Camel REST DSL lets you expose REST endpoints without writing controller code. Configure a servlet component and define endpoints with rest()
<!-- Add the servlet component for REST DSL with Spring Boot -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
</dependency>
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;
@Component
public class RestApiRoute extends RouteBuilder {
@Override
public void configure() {
// Configure the REST component once per application
restConfiguration()
// use the servlet component (Spring Boot embedded Tomcat)
.component("servlet")
.bindingMode(RestBindingMode.json) // auto-marshal/unmarshal JSON
.dataFormatProperty("prettyPrint", "true")
.contextPath("/api")
.port(8080);
// Define REST endpoints
rest("/hello")
.get("/")
.description("Say hello")
.outType(String.class)
.to("direct:hello")
.post("/")
.type(String.class)
.to("direct:saveHello");
// Route logic
from("direct:hello")
.setBody(constant("Hello from Apache Camel REST DSL!"));
from("direct:saveHello")
.log("Received: ${body}")
.setBody(constant("Saved"));
}
}
camel-servlet-startercontextPathHealth & Management
Add spring-boot-starter-actuator/actuator/camel
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
# Expose actuator endpoints
management.endpoints.web.exposure.include=health,info,camel
# Show Camel route details in health
camel.springboot.load-statistics-enabled=true
# Enable JMX management (useful with JConsole or Hawtio)
camel.springboot.jmx-management-enabled=true
// The /actuator/health endpoint will now include a camelHealth section:
// {
// "camelHealth": {
// "status": "UP",
// "camelContext": "MyIntegrationApp",
// "routes": { "total": 3, "started": 3, "stopped": 0 }
// }
// }
/actuator/health/camel/routes/<routeId>camel.springboot.load-statistics-enabled=true