The servlet: component provides HTTP based endpoints for consuming HTTP requests that arrive at a HTTP endpoint that is bound to a published Servlet.
Maven users will need to add the following dependency to their
pom.xml for this component:
You can append query options to the URI in the following format,
Reference to an
|Camel 2.16: Reference to an |
Whether or not the
Specifies the servlet name that the servlet endpoint will bind to. This name should match the name you define in
|Camel 2.11: Consumer only: Used to only allow consuming if the HttpMethod matches, such as GET/POST/PUT etc. From Camel 2.15 onwards multiple methods can be specified separated by comma.|
Camel will apply the same Message Headers as the HTTP component.
Camel will also populate all
request.headers. For example, if a client request has the URL,
http://myserver/myserver?orderid=123, the exchange will contain a header named
orderid with the value 123.
You can consume only from endpoints generated by the Servlet component. Therefore, it should be used only as input into your Camel routes. To issue HTTP requests against other HTTP endpoints, use the HTTP Component
Putting Camel JARs in the app server boot classpath
If you put the Camel JARs such as
camel-servlet, etc. in the boot classpath of your application server (eg usually in its lib directory), then mind that the servlet mapping list is now shared between multiple deployed Camel application in the app server.
Mind that putting Camel JARs in the boot classpath of the application server is generally not best practice!
So in those situations you must define a custom and unique servlet name in each of your Camel application, eg in the
And in your Camel endpoints then include the servlet name as well
From Camel 2.11 onwards Camel will detect this duplicate and fail to start the application. You can control to ignore this duplicate by setting the servlet init-parameter ignoreDuplicateServletName to true as follows:
But its strongly advised to use unique servlet-name for each Camel application to avoid this duplication clash, as well any unforeseen side-effects.
In this sample, we define a route that exposes a HTTP service at
First, you need to publish the CamelHttpTransportServlet through the normal Web Container, or OSGi Service.
Web.xml file to publish the CamelHttpTransportServlet as follows:
Then you can define your route as follows:
Sample when using Spring 3.x
Sample when using Spring 2.x
When using the Servlet component in a Camel/Spring application it's often required to load the Spring ApplicationContext after the Servlet component has started. This can be accomplished by using Spring's
ContextLoaderServlet instead of
ContextLoaderListener. In that case you'll need to start
ContextLoaderServlet after CamelHttpTransportServlet like this:
Sample when using OSGi
From Camel 2.6.0, you can publish the CamelHttpTransportServlet as an OSGi service with help of SpringDM like this.
Then use this service in your camel route like this:
For versions prior to Camel 2.6 you can use an
Activator to publish the CamelHttpTransportServlet on the OSGi platform