sleepy_penguin
sleepy_penguin

Reputation: 63

Best way to define restConfiguration() with multiple rest dsl route builders in Apache Camel

I am relatively new to camel and I'm looking for a good way to define multiple route builders using the REST DSL component in camel while using one restConfiguration() definition as this should remain the same across all route builders.

I found this question (Connecting multiple Camel Routes in REST DSL) in which someone linked to a base class defining restConfiguration() which will be automatically called by child classes (https://github.com/RovoMe/camel-rest-dsl-with-spring-security/blob/master/src/main/java/at/rovo/awsxray/routes/api/BaseAPIRouteBuilder.java) which is desirable as we only want to define the restConfiguration if there is a rest consumer route defined, but this would mean the restConfiguration is called multiple times (for each child); is this the best solution?

Thanks for your time!

Upvotes: 1

Views: 5931

Answers (1)

I don't think so.

By using Spring Boot and annotating your REST route classes with @Component, you'll be able to separate the REST configuration from the REST service(s) but all of them will be part of the same Camel context.

@Component
class MyRestConfig extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        restConfiguration()
            .contextPath("/my-api")
            .apiContextPath("/doc")
            .bindingMode(RestBindingMode.json)
            ...
        ;
    }
}

@Component
class BooksService extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        rest("/books").description("Books-related service")

        .get()
            .description("Lists Books according to the search parameters.")
            .produces("application/json")
            .route()
                .routeId("api-books-search")
                .to("direct://books-service-core")
            .end()
        .endRest();
    }

}

Upvotes: 1

Related Questions