1

Using Camel 2.19.3 REST DSL with Spring Boot

We want a Camel route to listen on the endpoint /myservice

(note, No base path like /rest/myservice or /camel/myservice)

Meanwhile, we also want the Spring Boot actuator endpoints to work, e.g. /health

Is there a way to do this, if we use Camel Servlet as the component?

Our web.xml does not work with:

<servlet>
    <servlet-name>CamelServlet</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>CamelServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

The Camel REST service works, but the Spring actuator endpoints do not.

Oleg Kurbatov
  • 1,376
  • 1
  • 19
  • 32
Darius X.
  • 2,886
  • 4
  • 24
  • 51

1 Answers1

3

You can make a bean of CamelServlet. In this case it will be picked up and regestered to the servlet container.

You can register servlets from annotation-based configuration using ServletRegistrationBean.

@Bean
public ServletRegistrationBean camelServletRegistrationBean(){
    return new ServletRegistrationBean(new CamelHttpTransportServlet(),"/*");
}

But in both cases the servlet will be regestered in the servlet container and the routing to the servlet will be handled by that servlet container. Spring will not decide whether to process a request with actuator or forward it to the camel servlet because the choise between CamelHttpTransportServlet and DispatcherServlet (Spring) will be made before the request is handed to the DispatcherServlet (Spring).

Hence the servlet mapping should be different at the servlet container level. If you mapp both of servlets to the root (<url-pattern>/</url-pattern>), the servlet container will not be able to decide which request should go to which servlet.

Oleg Kurbatov
  • 1,376
  • 1
  • 19
  • 32
  • Oleg, I think you're saying that what I want cannot be done because either way the Camel servlet would be "competing" with the DisatcherServlet? – Darius X. Jan 22 '18 at 00:08
  • 1
    Basically yes, it is not possible to map several servlets to root (`/*`) and get them working only on paths they can process without doing some hacky stuff like writing custom filters that forward requests depending on url. But I wouldn't do that myself and don't suggest you to. – Oleg Kurbatov Jan 22 '18 at 09:08
  • 1
    Maybe [this answer](https://stackoverflow.com/a/23014248/1497060) will help to construct servlet mappings using regular expressions. – Oleg Kurbatov Jan 22 '18 at 09:12
  • Thanks, that answer is helpful. Even though I won;t sue it in my specific use-case, I can see some possibilities elsewhere. Thanks for you help. – Darius X. Jan 22 '18 at 11:18