Building RESTful APIs with Apache Camel and Spring Boot
How Camel’s REST DSL can simplify API creation and mediation.
Introduction
Building RESTful APIs can be straightforward with Spring Boot alone—but when it comes to integrating with various systems, transforming messages, or adding enterprise-level routing logic, you need more. Apache Camel fits this niche perfectly.
Camel’s REST DSL (Domain-Specific Language) allows you to define REST endpoints and connect them to powerful integration flows with minimal effort. Combined with Spring Boot, it gives you a scalable, production-ready solution.
This article walks you through building a RESTful API using Camel’s REST DSL within a Spring Boot application and highlights its benefits over traditional approaches.
Why Use Apache Camel for REST APIs?
✅ Key Benefits:
- Declarative REST routes via
rest()DSL - Built-in integration patterns (e.g., routing, transformation)
- Flexible backends: files, databases, Kafka, JMS, etc.
- Seamless Spring Boot support
- Centralized error handling and logging
Setup: Spring Boot + Camel Dependencies
Add these dependencies to your pom.xml:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>4.4.0</version> <!-- Use latest -->
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-starter</artifactId>
</dependency>
Configure application properties:
camel:
springboot:
name: camel-api-demo
component:
servlet:
mapping:
context-path: /api/*
Define a Simple REST Endpoint with Camel DSL
Here’s how to expose a simple /greet endpoint:
@Component
public class RestApiRoute extends RouteBuilder {
@Override
public void configure() {
restConfiguration()
.component("servlet")
.bindingMode(RestBindingMode.auto);
rest("/greet")
.get("/{name}")
.produces("application/json")
.route()
.process(exchange -> {
String name = exchange.getIn().getHeader("name", String.class);
exchange.getMessage().setBody(Map.of("message", "Hello, " + name + "!"));
});
}
}
Explanation:
restConfiguration()sets up the REST component.rest("/greet")defines a GET route with a dynamic path.process()uses Camel’s processor to build the response.
Mediation with Camel
Now let’s say you want to route incoming data to a file or transform it before persisting. Camel makes this easy.
rest("/orders")
.post()
.consumes("application/json")
.to("direct:processOrder");
from("direct:processOrder")
.log("Received order: ${body}")
.marshal().json(JsonLibrary.Jackson)
.to("file:data/orders?fileName=order-${date:now:yyyyMMddHHmmss}.json");
This flow:
- Accepts an order JSON.
- Logs it.
- Converts it to a JSON string.
- Writes it to a file.
Error Handling
You can define a global error handler like this:
onException(Exception.class)
.handled(true)
.log("Error: ${exception.message}")
.setBody(constant("An internal error occurred"))
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500));
Testing Your API
Use Postman or curl to test:
curl http://localhost:8080/api/greet/John
# Response: {"message": "Hello, John!"}
curl -X POST http://localhost:8080/api/orders \
-H "Content-Type: application/json" \
-d '{"id":123, "item":"Laptop"}'
Real-World Use Cases
Camel REST DSL is ideal for:
- API gateways that route to different microservices
- Data ingestion APIs writing to Kafka, S3, or DB
- System integration between legacy and cloud apps
- IoT device data collectors exposing telemetry REST endpoints
Conclusion
Apache Camel with Spring Boot is a powerful combination for building RESTful APIs with complex routing and integration requirements. Camel’s REST DSL provides a clean, readable way to define endpoints while giving you access to the full power of the Camel ecosystem.
It’s not just about exposing endpoints — it’s about creating smart, manageable API pipelines.
Useful Resources
- Apache Camel REST DSL Docs
- Camel + Spring Boot Examples
- Enterprise Integration Patterns
- Camel RouteBuilder Guide
- Spring Boot Docs




