转自:http://simpleframework.net/blog/v/57763.html
HTTP协议中,HTTP 头部字段是请求和响应消息头的组成部分。它们定义HTTP事务的操作参数,携带有客户端浏览器、请求压面、服务器以及其他更多的信息。
下面是使用CXF(v2.4.0)为Web服务增加HTTP headers的示例:
/**
* @author Singaram Subramanian
*
*/
/* Create a ClientProxyFactoryBean reference and assign it an instance of JaxWsProxyFactoryBean, a factory for creating JAX-WS proxies.
This class provides access to the internal properties used to set-up proxies. Using it provides more control than the standard JAX-WS APIs. */
ClientProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(singz.ws.cxf.sample.SampleServiceInterface.class);
// Set the web service endpoint URL here
factory.setAddress("http://xxx.xxx.com/services/SampleService/v1");
SampleServiceInterface serviceClient = (SampleServiceInterface) factory.create();
// Get the underlying Client object from the proxy object of service interface
Client proxy = ClientProxy.getClient(serviceClient);
// Creating HTTP headers
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("XXX-SOA-SERVICE-NAME", Arrays.asList("SampleService"));
headers.put("XXX-SOA-APP-NAME", Arrays.asList("SampleServiceAppv1"));
// Add HTTP headers to the web service request
proxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
// If you want to log the SOAP XML of outgoing requests and
// incoming responses at client side, you can leave this uncommented. It'll be helpful in debugging.
proxy.getOutInterceptors().add(new LoggingOutInterceptor());
proxy.getInInterceptors().add(new LoggingInInterceptor());
如果所用的 CXF 版本下于 2.4.0,使用MultivaluedMap,将发现定制的 Http headers会被或略,而在 CXF 论坛已发现一Bug请求认为这是一个问题:定制的Http headers 会遗失,若 CXF 拦截器不使用 MultivaluedMap(https://issues.apache.org/jira/browse/CXF-3408)。