向SOAP请求添加HTTP头


让我们假设我们想做一个SOAP拨打服务电话http://localhost:8080/samplewebservices/echoserviceinterface,它要求我们添加一个应用编程接口/访问令牌作为一个超文本传输协议头,我们可以这样做:

假设

端点:http://localhost:8080/samplewebservices/echoserviceinterface
服务接口:singz . ws . test . Interface . echo service
访问_令牌:046 B6 c7f-0b8a-43 B9-b35d-6489 E6 dae91

Spring应用程序上下文配置

<?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>

要添加的自定义拦截器HTTP headers出站SOAP调用

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);
 }

}

这里还有一个不使用拦截器的例子:http://singztechmusings.wordpress.com/2011/09/17/apache-cxf-how-to-add-custom-http-headers-to-a-web-service-request/