Adding HTTP Headers to a SOAP Request
Let’s assume that we want to make a SOAP call to a service at http://localhost:8080/samplewebservices/echoserviceinterface, and it requires that we add an API / Access token as an HTTP header, we can do it this way:
Assumptions
Endpoint: http://localhost:8080/samplewebservices/echoserviceinterface
Service Interface: singz.ws.test.interface.EchoService
ACCESS_TOKEN: 046b6c7f-0b8a-43b9-b35d-6489e6daee91 (HTTP Header)
Spring Application Context Configuration
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <jaxws:client id="echoServiceClient" serviceClass="singz.ws.test.interface.EchoService" address="http://localhost:8080/samplewebservices/echoserviceinterface" /> <cxf:bus> <cxf:outInterceptors> <bean class="singz.ws.test.echoserviceconsumer.outinterceptor.HttpHeaderInterceptor" /> </cxf:outInterceptors> </cxf:bus> </beans>
Custom Interceptor to add HTTP headers to outbound SOAP call
singz.ws.test.echoserviceconsumer.outinterceptor.HttpHeaderInterceptor.java
package singz.ws.test.echoserviceconsumer.outinterceptor; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; public class HttpHeaderInterceptor extends AbstractPhaseInterceptor<Message>{ public HttpHeaderInterceptor() { super(Phase.POST_LOGICAL); } public void handleMessage(Message message) { Map<String, List<String>> headers = new HashMap<String, List<String>>(); headers.put("ACCESS_TOKEN", Arrays.asList("046b6c7f-0b8a-43b9-b35d-6489e6daee91")); message.put(Message.PROTOCOL_HEADERS, headers); } }
Here’s another example without using interceptor: http://singztechmusings.wordpress.com/2011/09/17/apache-cxf-how-to-add-custom-http-headers-to-a-web-service-request/