Chapter 5. Deploying an Apache Camel Servlet Endpoint
Abstract
5.1. Apache Camel Servlet Example
Overview
url-pattern setting from web.xml, and the endpoint URI of the Camel servlet endpoint are combined to give the URL, http://localhost:8080/camel-example-servlet-tomcat-2.15.1.redhat-621084/camel/hello.
Figure 5.1. Camel Servlet Example Deployed in a Web Server
camel-example-servlet-tomcat example
examples/camel-example-servlet-tomcat directory. For details of how to install the Apache Camel distribution, see the section called “Install Apache Camel”.
Camel servlet component
org.apache.camel.component.servlet.CamelHttpTransportServlet
servlet://RelativePath[?Options]
RelativePath specifies the tail segment of the HTTP URL path for this servlet.
web.xml file
web.xml file. In the camel-example-servlet-tomcat project, the web.xml file is stored at the following location:
camel-example-servlet-tomcat/src/main/webapp/WEB-INF/web.xml
web.xml file.
Example 5.1. web.xml File for the camel-example-servlet-tomcat Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Web Application</display-name>
<!-- location of spring xml files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:camel-config.xml</param-value>
</context-param>
<!-- the listener that kick-starts Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Camel servlet -->
<servlet>
<servlet-name>CamelServlet</servlet-name>
<servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Camel servlet mapping -->
<servlet-mapping>
<servlet-name>CamelServlet</servlet-name>
<url-pattern>/camel/*</url-pattern>
</servlet-mapping>
</web-app>web.xml file are:
servlet/servlet-class- Specifies the
org.apache.camel.component.servlet.CamelHttpTransportServletclass, which implements the Camel servlet component. servlet-mapping/url-pattern- Determines which URLs are routed to this servlet. In general, the servlet URL has the following form:
http://Host:Port/WARFileName/URLPattern
Where the base URL,http://Host:Port, is determined by the configuration of the Web server, theWARFileNameis the root of theWARFileName.warWAR file, and theURLPatternis specified by the contents of theurl-patternelement.Assuming that the Web server port is set to 8080, thecamel-example-servlet-tomcatexample servlet will match URLs of the following form:http://localhost:8080/camel-example-servlet-tomcat-2.15.1.redhat-621084/camel/*
listener/listener-class- This element launches the Spring container.
context-param- This element specifies the location of the Spring XML file,
camel-config.xml, in the WAR. The Spring container will read this parameter and load the specified Spring XML file, which contains the definition of the Camel route.
Example Camel route
Example 5.2. Route Definition for the Camel Servlet Example
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<!-- incoming requests from the servlet is routed -->
<from uri="servlet:///hello"/>
<choice>
<when>
<!-- is there a header with the key name? -->
<header>name</header>
<!-- yes so return back a message to the user -->
<transform>
<simple>Hello ${header.name} how are you?</simple>
</transform>
</when>
<otherwise>
<!-- if no name parameter then output a syntax to the user -->
<transform>
<constant>Add a name parameter to uri, eg ?name=foo</constant>
</transform>
</otherwise>
</choice>
</route>
</camelContext>
</beans>servlet:///hello, specifies the relative path, /hello, the complete URL to access this servlet is the following:
http://localhost:8080/camel-example-servlet-tomcat-2.15.1.redhat-621084/camel/hello

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.