Reputation: 31
I followed this guide "Consuming a SOAP web service", at https://spring.io/guides/gs/consuming-web-service/ and changed it to call my own internal SOAP service, it makes the call as expected, however now I need to pass an http header via the WsTemplate, what is the easiest way to do this?
Upvotes: 1
Views: 4429
Reputation: 151
I've faced the same problem. If it can help someone, I've found a solution here: Spring WS Add Soap Header in Client
The idea is to create a class implementing org.springframework.ws.client.core.WebServiceMessageCallback
and override the doWithMessage()
method.
The doItMessage()
method takes a WebServiceMessage
as argument and is invoked by the springWs
process before sending the request, allowing to modify it before it is send.
What is done in the example above is marshalling the object and adding it to the header of the request.
In my case I have to be careful with XML annotations of the object to be set as header, especially the @XmlRootElement with the namespace attribute.
Once this is done, the WSClient
has to be adjusted to use the marshalSendAndReceive()
method that takes a request and an uri, a payload object, and a WebServiceMessageCallback
.
Upvotes: 1
Reputation: 31
public class WsHttpHeaderCallback implements WebServiceMessageCallback {
public WsHttpHeaderCallback()
{
super();
}
@Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException
{
String headerKey="headerkey";
String headerValue="headervalue";
addRequestHeader(headerKey, headerValue);
}
private void addRequestHeader(String headerKey, String headerValue) throws IOException
{
TransportContext context = TransportContextHolder.getTransportContext();
WebServiceConnection connection = context.getConnection();
if (connection instanceof HttpUrlConnection) {
HttpUrlConnection conn = (HttpUrlConnection) connection;
conn.addRequestHeader(headerKey, headerValue);
}
}
}
Upvotes: 2
Reputation: 321
I'm not sure if this helps but found some documentation
For setting WS-Addressing headers on the client, you can use the org.springframework.ws.soap.addressing.client.ActionCallback. ...
webServiceTemplate.marshalSendAndReceive(o, new ActionCallback("http://samples/RequestOrder"));
Upvotes: 0