Spring Web Services Component
Available as of Camel 2.6
The spring-ws: component allows you to integrate with Spring Web Services. It offers both client-side support, for accessing web services, and server-side support for creating your own contract-first web services.
Maven users will need to add the following dependency to their
pom.xml for this component:
The URI scheme for this component is as follows
To expose a web service mapping-type needs to be set to any of the following:
Offers the option to map web service requests based on the qualified name of the root element contained in the message.
Used to map web service requests based on the SOAP action specified in the header of the message.
In order to map web service requests that target a specific URI.
Used to map web service requests based on the evaluation of an XPath
expression against the incoming message. The result of the evaluation should match the XPath result specified in the endpoint URI.
Allows you to reference an
org.apache.camel.component.spring.ws.bean.CamelEndpointDispatcher object in order to integrate with existing (legacy) endpoint mappings like
As a consumer the address should contain a value relevant to the specified mapping-type (e.g. a SOAP action, XPath expression). As a producer the address should be set to the URI of the web service your calling upon.
You can append query options to the URI in the following format,
SOAP action to include inside a SOAP request when accessing remote web services
WS-Addressing 1.0 action header to include when accessing web services. The
To header is set to the address of the web service as specified in the endpoint URI (default Spring-WS behavior).
Only when mapping-type is
XPath expression to use in the process of mapping web service requests, should match the result specified by
Camel 2.10: Sets the socket read timeout (in milliseconds) while invoking a webservice using the producer, see URLConnection.setReadTimeout() and CommonsHttpMessageSender.setReadTimeout(). This option works when using the built-in message sender implementations: CommonsHttpMessageSender and HttpUrlConnectionMessageSender. One of these implementations will be used by default for HTTP based services unless you customize the Spring WS configuration options supplied to the component. If you are using a non-standard sender, it is assumed that you will handle your own timeout configuration.
Camel 2.12: The built-in message sender HttpComponentsMessageSender is considered instead of CommonsHttpMessageSender which has been deprecated, see HttpComponentsMessageSender.setReadTimeout().
Camel 2.10: Reference to an
org.apache.camel.util.jsse.SSLContextParameters in the Registry. See Using the JSSE Configuration Utility. This option works when using the built-in message sender implementations: CommonsHttpMessageSender and HttpUrlConnectionMessageSender. One of these implementations will be used by default for HTTP based services unless you customize the Spring WS configuration options supplied to the component. If you are using a non-standard sender, it is assumed that you will handle your own TLS configuration.
Camel 2.12: The built-in message sender HttpComponentsMessageSender is considered instead of CommonsHttpMessageSender which has been deprecated.
Registry based options
The following options can be specified in the registry (most likely a Spring ApplicationContext) and referenced from the endpoint URI using the # notation.
Option to provide a custom WebServiceTemplate. This allows for full control over client-side web services handling; like adding a custom interceptor or specifying a fault resolver, message sender or message factory.
Option to provide a custom WebServiceMessageSender. For example to perform authentication or use alternative transports
Option to provide a custom WebServiceMessageFactory. For example when you want Apache Axiom to handle web service messages instead of SAAJ
Option to override default TransformerFactory. The provided transformer factory must be of type
Only when mapping-type is
Reference to an instance of
org.apache.camel.component.spring.ws.bean.CamelEndpointMapping in the Registry/ApplicationContext. Only one bean is required in the registry to serve all Camel/Spring-WS endpoints. This bean is auto-discovered by the MessageDispatcher and used to map requests to Camel endpoints based on characteristics specified on the endpoint (like root QName, SOAP action, etc)
Camel 2.10.3 Option to provide a custom MessageFilter. For example when you want to process your headers or attachments by your own.
URI of the web service your accessing as a client, overrides address part of the endpoint URI
Header to specify the SOAP action of the message, overrides
soapAction option if present
Use this header to specify the WS-Addressing action of the message, overrides
wsAddressingAction option if present
Camel 2.11.1: Use this header to specify/access the SOAP headers of the message.
Accessing web services
To call a web service at
http://foo.com/bar simply define a route:
And sent a message:
Remember if it's a SOAP service you're calling you don't have to include SOAP tags. Spring-WS will perform the XML-to-SOAP marshaling.
Sending SOAP and WS-Addressing action headers
When a remote web service requires a SOAP action or use of the WS-Addressing standard you define your route as:
Optionally you can override the endpoint options with header values:
Available as of Camel 2.11.1
You can provide the SOAP header(s) as a Camel Message header when sending a message to a spring-ws endpoint, for example given the following SOAP header in a String
We can set the body and header on the Camel Message as follows:
And then send the Exchange to a
spring-ws endpoint to call the Web Service.
Likewise the spring-ws consumer will also enrich the Camel Message with the SOAP header.
For an example see this unit test.
The header and attachment propagation
Spring WS Camel supports propagation of the headers and attachments into Spring-WS WebServiceMessage response since version 2.10.3. The endpoint will use so called "hook" the MessageFilter (default implementation is provided by BasicMessageFilter) to propagate the exchange headers and attachments into WebServiceMessage response. Now you can use
Note: If the exchange header in the pipeline contains text, it generates Qname(key)=value attribute in the soap header. Recommended is to create a QName class directly and put into any key into header.
How to use MTOM attachments
The BasicMessageFilter provides all required information for Apache Axiom in order to produce MTOM message. If you want to use Apache Camel Spring WS within Apache Axiom, here is an example:
1. Simply define the messageFactory as is bellow and Spring-WS will use MTOM strategy to populate your SOAP message with optimized attachments.
2. Add into your pom.xml the following dependencies
3. Add your attachment into the pipeline, for example using a Processor implementation.
4. Define endpoint (producer) as ussual, for example like this:
5. Now, your producer will generate MTOM message with otpmized attachments.
The custom header and attachment filtering
If you need to provide your custom processing of either headers or attachments, extend existing BasicMessageFilter and override the appropriate methods or write a brand new implementation of the MessageFilter interface.
To use your custom filter, add this into your spring context:
You can specify either a global a or a local message filter as follows:
a) the global custom filter that provides the global configuration for all Spring-WS endpoints
b) the local messageFilter directly on the endpoint as follows:
For more information see CAMEL-5724
If you want to create your own MessageFilter, consider overriding the following methods in the default implementation of MessageFilter in class BasicMessageFilter:
Using a custom MessageSender and MessageFactory
A custom message sender or factory in the registry can be referenced like this:
Exposing web services
In order to expose a web service using this component you first need to set-up a MessageDispatcher to look for endpoint mappings in a Spring XML file. If you plan on running inside a servlet container you probably want to use a
MessageDispatcherServlet configured in
By default the
MessageDispatcherServlet will look for a Spring XML named
/WEB-INF/spring-ws-servlet.xml. To use Camel with Spring-WS the only mandatory bean in that XML file is
CamelEndpointMapping. This bean allows the
MessageDispatcher to dispatch web service requests to your routes.
More information on setting up Spring-WS can be found in Writing Contract-First Web Services. Basically paragraph 3.6 "Implementing the Endpoint" is handled by this component (specifically paragraph 3.6.2 "Routing the Message to the Endpoint" is where
CamelEndpointMapping comes in). Also don't forget to check out the Spring Web Services Example included in the Camel distribution.
Endpoint mapping in routes
With the XML configuration in-place you can now use Camel's DSL to define what web service requests are handled by your endpoint:
The following route will receive all web service requests that have a root element named "GetFoo" within the
The following route will receive web service requests containing the
http://example.com/GetFoo SOAP action.
The following route will receive all requests sent to
The route below will receive requests that contain the element
<foobar>abc</foobar> anywhere inside the message (and the default namespace).
Alternative configuration, using existing endpoint mappings
For every endpoint with mapping-type
beanname one bean of type
CamelEndpointDispatcher with a corresponding name is required in the Registry/ApplicationContext. This bean acts as a bridge between the Camel endpoint and an existing endpoint mapping like
An example of a route using
Camel's pluggable data formats offer support for pojo/xml marshalling using libraries such as JAXB, XStream, JibX, Castor and XMLBeans. You can use these data formats in your route to sent and receive pojo's, to and from web services.
When accessing web services you can marshal the request and unmarshal the response message:
Similarly when providing web services, you can unmarshal XML requests to POJO's and marshal the response message back to XML: