Question

I need to get the SOAP message from exchange object I receive in my spring bean.

I have a camel route, which routes from service endpoint to my java bean. Java Bean and Camel route declared in spring looks like this:

<bean id="processor" class="com.groupgti.esb.camel.wrapper.gradireland.userregistration.UserRegistrationProcessor">

<camel:route id="route">
    <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=PAYLOAD&amp;synchronous=true" />
    <camel:bean ref="processor" />
    <camel:to uri="cxf:bean:myTargetEndpoint"/>
</camel:route>

In my java bean I receive the exchange object:

@Override
public SOAPMessage processMessage(Exchange exchange) {
    Object object = exchange.getIn().getHeaders().get("CamelCxfMessage");
    LOGGER.debug("Object: " + object);
    SOAPMessage message = null;

    if (object instanceof SOAPMessage) {
        message = (SOAPMessage) object;
        LOGGER.debug("Got message: " + message);
    }

    LOGGER.debug("Sending message...");
    return message;
}

The problem is that I can not get the SOAP message out of the exchange. I camel web site, here I found that I have to use this to get the SOAP message:

SOAPMessage soapMessage = (SOAPMessage) exchange.getIn().getBody(List.class).get(0);

But his gives me NullPointerException somewhere deep in exchange.

I have tried to debug and see the object tree. I found that I can get the message like this:

SOAPMessage soapMessage = (SOAPMessage) exchange.getIn().getHeaders().get("CamelCxfMessage");

But this gives me this exception:

org.apache.camel.ExpectedBodyTypeException: Could not extract IN message body as type: interface javax.xml.transform.Source body is: null

I am stuck here. Maybe someone know where can be the problem?

Was it helpful?

Solution

PAYLOAD mode is not quite the easy way, maybe you should try the POJO mode for CXF, and get rid of SoapMessage. You will have to declare a POJO with JAXB annotations, like

@XmlAccessorType(XmlAccessType.FIELD)
public class Registration {

    private Long roomNumber;
    ...

}

This will allow you to work directly on the Registration class in your processors (which I assume is your final goal).

Registration registration = exchange.getIn().getBody(Registration.class);

If you persist with PAYLOAD mode, note that you can write

SOAPMessage soapMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, SOAPMessage.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top