Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding HTTP Headers to a SOAP Request

DZone's Guide to

Adding HTTP Headers to a SOAP Request

We'll use a custom CXF interceptor to add these headers.

Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

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/


Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
java ,enterprise-integration ,integration ,apache cxf

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}