Integration / Apache Camel Interview Questions
How do you use the Camel REST DSL to expose and consume REST services?
The REST DSL is a domain-specific language layered on top of Camel HTTP transport components. It describes REST APIs in a declarative verb-and-path style. The underlying HTTP server is pluggable — Undertow, Jetty, Servlet, or Netty — chosen via restConfiguration().
public class OrderRestRoutes extends RouteBuilder {
@Override
public void configure() {
restConfiguration()
.component("undertow").host("0.0.0.0").port(8080);
rest("/api/orders")
.get("/{id}").produces("application/json")
.to("direct:getOrder")
.post("/").consumes("application/json")
.type(OrderRequest.class)
.to("direct:createOrder");
from("direct:getOrder")
.to("sql:SELECT * FROM orders WHERE id = :#${header.id}");
}
}
// Consuming an external REST endpoint:
from("direct:callProducts")
.to("rest:GET:/api/products?host=catalog.internal:8080");REST DSL routes handle JSON/XML binding automatically when a type() class is specified. The rest: component acts as a producer for calling external REST APIs. Use camel-openapi-java to generate an OpenAPI spec from REST DSL definitions. In Spring Boot, auto-configure the REST engine via camel.rest.component=servlet.
More Related questions...