SOAP request in Spring 4: Add Header to SOAP message

Spring ‘Convention over configuration‘ routine makes it easy to implement basic use cases. But when it comes to advanced configuration, sometimes it can be difficult (see post about authenticating SOAP requests).

Another case is when you need to add SOAP Header to the SOAP message. This time it is however much easier than authenticating requests.

Sending the basic request

Developer has access to the request after it is marshalled, but before it is send. This is the moment when you can modify message by adding header. Following the spring.io site tutorial, you probably know that sending request is done via:

getWebServiceTemplate().marshalSendAndReceive(
	request,
	someSoapActionCallback);

Create class that will modify the request

The someSoapActionCallback will be called after the request is marshalled, and before it is send, allowing you to modify it before sending. So this is a hook where you can modify step of an sending algorythm. This is an instance of a class implementing the WebServiceMessageCallback and overriding its method: doWithMessage(WebServiceMessage message). This is how my class looks like:

public class SoapRequestHeaderModifier implements WebServiceMessageCallback{
	
	@Override
	public void doWithMessage(WebServiceMessage message) throws IOException,
			TransformerException {
		
		SoapHeader header = ((SoapMessage)message).getSoapHeader();
		
		StringSource headerSource = new StringSource(prepareSecurityHeader());
        
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(headerSource, header.getResult());
	}
	
	private String prepareSecurityHeader() {
		String result = "";
		StringWriter sw = new StringWriter();
        try {
            JAXBContext carContext = JAXBContext.newInstance(Security.class);
            Marshaller carMarshaller = carContext.createMarshaller();
            carMarshaller.marshal(SECURITY_HEADER, sw);
            result = sw.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
		return result;
	}
}

Where SECURITY_HEADER is an authentication string required by SOAP server. The Security.class is a data class generated from JAX2B (marshalled from WSDL). And Security class is in fact being added to the SOAP header.

Use the Header Modifier while sending request

I annotated the Header Modifier as a Component and Atowired its instance in a constructor. Thanks to that I can add its instance to the marshall request as folows:

getWebServiceTemplate().marshalSendAndReceive(
       request, 
       soapRequestHeaderModifier);

Now you can ensure that your request header is modified using your favorite SOAP mock client (SoapUI for instance).

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

One thought on “SOAP request in Spring 4: Add Header to SOAP message

Give Your feedback: