Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Chapter 128. SERVLET
Servlet Component Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The servlet: component provides HTTP based endpoints for consuming HTTP requests that arrive at a HTTP endpoint and this endpoint is bound to a published Servlet.
Maven users will need to add the following dependency to their
pom.xml
for this component:
URI format Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
servlet://relative_path[?options]
servlet://relative_path[?options]
You can append query options to the URI in the following format,
?option=value&option=value&...
Options Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Name | Default Value | Description |
---|---|---|
httpBindingRef
|
null
|
Reference to an org.apache.camel.component.http.HttpBinding in the Registry. A HttpBinding implementation can be used to customize how to write a response.
|
matchOnUriPrefix
|
false
|
Whether or not the CamelServlet should try to find a target consumer by matching the URI prefix, if no exact match is found.
|
servletName
|
CamelServlet
|
Specifies the servlet name that the servlet endpoint will bind to. If there is no servlet name specified, the servlet endpoint will be bind to first published Servlet. |
httpMethodRestrict
|
null
|
Camel 2.11: (Consumer only) Used to only allow consuming if the HttpMethod matches, such as GET /POST /PUT , and so on. From Camel 2.15 onwards, multiple methods can be specified, separated by a comma.
|
Message Headers Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Apache Camel will apply the same Message Headers as the HTTP component.
Apache Camel will also populate all
request.parameter
and 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.
Usage Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
You can only consume from endpoints generated by the Servlet component. Therefore, it should only be used as input into your Apache Camel routes. To issue HTTP requests against other HTTP endpoints, use the HTTP Component
Putting Camel JARs in the app server boot classpath Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
If you put the Camel JARs such as
camel-core
, 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
web.xml
define:
And in your Camel endpoints then include the servlet name as well
<route> <from uri="servlet://foo?servletName=MyServlet"/> ... </route>
<route>
<from uri="servlet://foo?servletName=MyServlet"/>
...
</route>
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.
Sample Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Important
From Camel 2.7 onwards its easier to use Servlet in Spring web applications. See Servlet Tomcat Example for details.
In this sample, we define a route that exposes a HTTP service at
http://localhost:8080/camel/services/hello
. First, you need to publish the CamelHttpTransportServlet through the normal Web Container, or OSGi Service. Use the Web.xml
file to publish the CamelHttpTransportServlet as follows:
Then you can define your route as follows:
Specify the relative path for camel-servlet endpoint
Since we are binding the Http transport with a published servlet, and we don't know the servlet's application context path, the
camel-servlet
endpoint uses the relative path to specify the endpoint's URL. A client can access the camel-servlet
endpoint through the servlet publish address: ("http://localhost:8080/camel/services") + RELATIVE_PATH("/hello")
.
Sample when using Spring 3.x Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The standalone Apache Camel package contains a demonstration of how to deploy the Servlet component in the Tomcat Web container. The demonstration is located in the
examples/camel-example-servlet-tomcat
directory. When deploying a Servlet component in the Web container, it is necessary to create a Spring application context explicitly by creating a Spring ContextLoaderListener
instance in the WEB-INF/web.xml
file.
For example, to create a Spring application context that loads Spring definitions (including the
camelContext
and route definitions) from the camel-config.xml
file, define a web.xml
file as follows:
Sample when using Spring 2.x Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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:
Alternatively - pre Camel 2.6 - you can use an
Activator
to publish the CamelHttpTransportServlet on the OSGi platform