Servlet Component
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:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-servlet</artifactId> <version>x.x.x</version> <\!-\- use the same version as your Camel core version \--> </dependency>
Stream
Servlet is stream based, which means the input it receives is submitted to Camel as a stream. That means you will only be able to read the content of the stream once. If you find a situation where the message body appears to be empty or you need to access the data multiple times (eg: doing multicasting, or redelivery error handling) you should use Stream caching or convert the message body to a String
which is safe to be read multiple times.
URI format
servlet://relative_path[?options]
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Name |
Default Value |
Description |
---|---|---|
|
|
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 |
Message Headers
Camel will apply the same Message Headers as the HTTP component.
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
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
Sample
From Camel 2.7 onwards it's 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
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:
<web-app> <servlet> <servlet-name>CamelServlet</servlet-name> <servlet-class> org.apache.camel.component.servlet.CamelHttpTransportServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>SpringApplicationContext</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <web-app>
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