Apache CXF: 如何为 Web 请求增加定制的 HTTP header?

转自: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)。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值