user366735 user366735 - 3 months ago 81
HTTP Question

Setting a custom HTTP header dynamically with Spring-WS client

How do you set a custom HTTP header (not SOAP header) dynamically on the client side when using Spring-WS?

Answer
public class AddHttpHeaderInterceptor implements ClientInterceptor {

public boolean handleFault(MessageContext messageContext)
        throws WebServiceClientException {
    return true;
}

public boolean handleRequest(MessageContext messageContext)
        throws WebServiceClientException {
    TransportContext context = TransportContextHolder.getTransportContext();
    CommonsHttpConnection connection = (CommonsHttpConnection) context.getConnection();
    PostMethod postMethod = connection.getPostMethod();
    postMethod.addRequestHeader( "fsreqid", "123456" );

    return true;
}

public boolean handleResponse(MessageContext messageContext)
        throws WebServiceClientException {
    return true;
}

}

config:

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    ...
    <property name="interceptors">
        <list>
            <bean class="com.blah.AddHttpHeaderInterceptor" />
        </list>
    </property>
</bean>