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.

XML
<!-- 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>
The Spring Boot auto-configuration creates and starts a CamelContext bean automatically. You never need to call context.start() yourself: Spring Boot manages the lifecycle.

Defining Routes with RouteBuilder

Annotate a class with @Component and extend RouteBuilder. Spring discovers it automatically and adds its routes to the CamelContext.

Java
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}");
    }
}
You can have multiple RouteBuilder beans in the same application: Camel collects them all. Use routeId() to name routes for easier monitoring and management.

Camel Properties in application.properties

Camel Spring Boot exposes camel.springboot.* properties. You can also use {{placeholders}} inside route URIs to inject values from application.properties.

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
Java
@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 via @Autowired or constructor injection. You can also reference beans by name in the route DSL with bean().

Java
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");
    }
}
Java
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);
    }
}
The bean() DSL method calls the specified method and replaces the exchange body with the return value. If the method has a parameter matching the exchange body type, Camel passes the body automatically.

REST DSL

The Camel REST DSL lets you expose REST endpoints without writing controller code. Configure a servlet component and define endpoints with rest().

XML
<!-- Add the servlet component for REST DSL with Spring Boot -->
<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-servlet-starter</artifactId>
</dependency>
Java
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"));
    }
}
With camel-servlet-starter and Spring Boot, the REST DSL automatically maps to the Spring Boot embedded server under the configured contextPath. No additional servlet registration is needed.

Health & Management

Add spring-boot-starter-actuator and Camel's actuator support to get /actuator/camel and Camel route health indicators.

XML
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Properties
# 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
Java
// The /actuator/health endpoint will now include a camelHealth section:
// {
//   "camelHealth": {
//     "status": "UP",
//     "camelContext": "MyIntegrationApp",
//     "routes": { "total": 3, "started": 3, "stopped": 0 }
//   }
// }
Camel also exposes individual route health checks under /actuator/health/camel/routes/<routeId>. Use camel.springboot.load-statistics-enabled=true to get exchange throughput and processing time in the actuator output.