Multiple servlets with Camel Servlet possible?

I want to expose 3 apis through my application. The pattern is as follows:

  • /api/ui/* (handled by Spring) maps to servlet 1
  • /api/api1/* (handled by Camel’s servlet component) maps to servlet 2
  • /api/api2/* (handled by Camel’s servlet component) maps to servlet 3

seems I cannot do it with Camel…

This is how I register the servlet 2:

@Bean
public ServletRegistrationBean camelServletRegistrationBean() {
    final CamelHttpTransportServlet camelServlet = new CamelHttpTransportServlet();
    final ServletRegistrationBean servletBean = new ServletRegistrationBean(camelServlet, "/api/api1/*");
    servletBean.setName("servlet2");
    return servletBean;
}

public class Api1RestInputRoute extends RouteBuilder {


   restConfiguration().component("servlet").componentProperty("servletName", "servlet2");

   rest()
      .consumes(MediaType.TEXT_PLAIN_VALUE)
      .get("/health").to("log:api1");
}

public class Api2RestInputRoute extends RouteBuilder {


   restConfiguration().component("servlet").componentProperty("servletName", "servlet3");

   rest()
      .consumes(MediaType.TEXT_PLAIN_VALUE)
      .get("/health").to("log:api2");
}

It looks like both restConfigurations set “servletName” property on the same component and the first wins.

I don’t want to have /api/camel/api1/, /api/camel/api2/ for Camel Servlet…

My Apache Camel version is 2.21.5

Answer

There are many ways to provide the configuration via rest dsl in Apache Camel. Thus I spent a lot of time with debugging to find out, that there is no chance (in 2.21.5) to select the servlet to use, if you want to have multiple.

The problem is that Camel on the one hand only supports the one and only “servlet” component, so it does not help if you add “servlet1” ServletComponent, it will still use “servlet” in the end.

Although you can specify a lot of RestConfigurations, the one and only configuration for “servlet” will be selected.

And if you try to use the query part of the RestEndpoint, then the problem is that the created ServletEndpoints with properly set servletNames will anyway collide with their uri: “servlet:/health?httpMethodRestrict=GET”, and thus you will have only one…

For REST I will need to switch to Spring 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *