16 November 2017

Apache Camel REST Endpoint and Servlet Name

This problem took me a few hours to solve at my current client.

Apache Camel provides a way to expose routes as REST endpoints:


restConfiguration().component("servlet");

rest("start")
        .get()
        .route().routeId("esb-parisbridge rest")
        .to("direct:start")
        .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
        .transform(constant("Completed Request successfully"));


Now when you are deploying multiple WARs into one app server the CamelHttpTransportServlet is there multiple times with the same name.
To make this work you need to rename the servlet name to a unique name:

<servlet>
    <display-name>Camel Http Transport Servlet</display-name>
    <servlet-name>CamelServletParisBridge</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>CamelServletParisBridge</servlet-name>
    <url-pattern>/camel/*</url-pattern>
</servlet-mapping>

The problem now is that Camel does not know about the changed name and you will get a 404 not found when trying to access the REST endpoint.

You must tell the Camel servlet component what the name of the servlet will be:

restConfiguration().component("servlet")
  .endpointProperty("servletName", "CamelServletParisBridge");

Keine Kommentare: