Unable to set SOAP header when calling web service via Camel using dataFormat as POJO


I am using Camel in our project and requesting WebServices, the dataFormat is POJO. I was able to request when my SOAP message did not contain SOAP headers, but when it had Headers, I was unable to set those. I looked at the documentation but was not able to understand and have several questions.

I want to create a message like the below:

<soapenv:Envelope`enter code here`
                <platformCore:status isSuccess="true"
                        <platformCore:status isSuccess="false"
                            <platformCore:statusDetail type="ERROR">
                                <platformCore:message>This entity already exists.</platformCore:message>
            </addListResponse>`enter code here`

I will be able to send the message if there was only Body, but can someone give me a code snippet for including the header section? The dataFormat is POJO.

When using CXF endpoint with dataFormat as POJO, body in Camel Exchange object is an object of org.apache.cxf.message.MessageContentsList. It is an extension of java.util.ArrayList<Object> and it contains parts of SOAP Message in order as defined in WSDL and corresponding method in WebService class. Element 0 there is a Body.

So, one way to do that with Java is to create a Processor class implementing org.apache.camel.Processor interface and in its process method set your SOAP header. Something like:

public void process(Exchange camelExchange) throws Exception {

  MessageContentsList messageBody = (MessageContentsList) camelExchange.getIn().getBody(); 

   DocumentInfo docInfoHeader = new DocumentInfo();
   ... set docInfoHeader properties ...


(sample is not tested. It is just an idea, how to handle that...)

Other answer on similar question you can find here: Setting Custom Soap Header-To Pojo Message In Camel Cxf

It describes how to use Camel Exchange headers as SOAP Headers.

I'm not sure for 100% which way will work for you and which one is better... I guess, it depends on WSDL you use.

UPD: second choice is to use pure CXF solution by using CxfMessageSoapHeaderOutInterceptor custom implementation. It may look like:

public class MyCxfInterceptor extends CxfMessageSoapHeaderOutInterceptor {
   public void handleMessage( org.apache.cxf.binding.soap.SoapMessage message) {

      org.apache.cxf.binding.soap.SoapHeader myCustomHeader = new org.apache.cxf.binding.soap.SoapHeader(new QName(
                {custom name space}, {custom local name}), {Custom content object}));




and set Interceptor in Camel Cxf Endpoint as :

<cxfEndpoint ...>
        <spring:bean class="MyCxfInterceptor"/>