SOAP DataFormat
Available as of Camel 2.3
SOAP is a Data Format which uses JAXB2 and JAX-WS to marshal and unmarshal SOAP payloads. It provides the basic features of Apache CXF without need for the CXF Stack.
ElementNameStrategy
An element name strategy is used for two purposes. The first is to find a xml element name for a given object and soap action when marshalling the object into a SOAP message. The second is to find an Exception class for a given soap fault name.
Strategy |
Usage |
QNameStrategy |
Uses a fixed qName that is configured on instantiation. Exception lookup is not supported |
TypeNameStrategy |
Uses the name and namespace from the @XMLType annotation of the given type. If no namespace is set then package-info is used. Exception lookup is not supported |
ServiceInterfaceStrategy |
Uses information from a webservice interface to determine the type name and to find the exception class for a SOAP fault |
If you have generated the web service stub code with cxf-codegen or a similar tool then you probably will want to use the ServiceInterfaceStrategy. In the case you have no annotated service interface you should use QNameStrategy or TypeNameStrategy.
Using the Java DSL
The following example uses a named DataFormat of soap which is configured with the package com.example.customerservice to initialize the JAXBContext. The second parameter is the ElementNameStrategy. The route is able to marshal normal objects as well as exceptions. (Note that this route defines a one way soap request. If you need request reply then you should look at the next example)
SoapJaxbDataFormat soap = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
from("direct:start")
.marshal(soap)
.to("jms:myQueue");
![]() | See also As the SOAP dataformat inherits from the JAXB dataformat most settings apply here as well |
Examples
Webservice client
The following route supports marshalling the request and unmarshalling a response or fault.
String WS_URI = "cxf:;
SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
from("direct:customerServiceClient")
.onException(Exception.class)
.handled(true)
.unmarshal(soapDF)
.end()
.marshal(soapDF)
.to(WS_URI)
.unmarshal(soapDF);
The below snippet creates a proxy for the service interface and makes a SOAP call to the above route.
import org.apache.camel.Endpoint;
import org.apache.camel.component.bean.ProxyHelper;
...
Endpoint startEndpoint = context.getEndpoint("direct:customerServiceClient");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
CustomerService proxy = ProxyHelper.createProxy(startEndpoint, classLoader, CustomerService.class);
GetCustomersByNameResponse response = proxy.getCustomersByName(new GetCustomersByName());
Webservice Server
Using the following route sets up a webservice server that listens on jms queue customerServiceQueue and processes requests using the class CustomerServiceImpl. The customerServiceImpl of course should implement the interface CustomerService. Instead of directly instantiating the server class it could be defined in a spring context as a regular bean.
SoapJaxbDataFormat soapDF = new SoapJaxbDataFormat("com.example.customerservice", new ServiceInterfaceStrategy(CustomerService.class));
CustomerService serverBean = new CustomerServiceImpl();
from("jms:)
.onException(Exception.class)
.handled(true)
.marshal(soapDF)
.end()
.unmarshal(soapDF)
.bean(serverBean)
.marshal(soapDF);
Dependencies
To use the SOAP dataformat in your camel routes you need to add the following dependency to your pom.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-soap</artifactId>
<version>2.3.0</version>
</dependency>