apache-cxf测试webservice添加header信息
2013-09-28 00:00
344 查看
使用apache-cxf自动生成webservice本地类,有时需要在soap中添加header信息,如下面
![](http://static.oschina.net/uploads/img/201309/28005650_V74X.jpg)
maven cxf-codegen-plugin插件测试webservice
<soap:header> <username>youthflies</username> <password>youthflies</username> </soap:header>我们可以在生成webservice client的时候,手动添加上我们需要的header信息。 新建一个
SoapHeaderInterceptor.java,内容如下:
package com.yeetrack.webservice; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapHeader; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.headers.Header; import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.Phase; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * @author youthflies * 自定义的soap拦截器,用来添加header信息 */ public class SoapHeaderInterceptor extends AbstractSoapInterceptor { public SoapHeaderInterceptor() { super(Phase.WRITE); } @Override public void handleMessage(SoapMessage message) throws Fault { // TODO Auto-generated method stub List headers=message.getHeaders(); headers.add(getHeader("username", "youthflies")); headers.add(getHeader("password", "youthflies")); } //http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl private Header getHeader(String key, String value) { QName qName=new QName("http://webservice.webxml.com.cn/", key); Document document=DOMUtils.createDocument(); Element element=document.createElementNS("http://webservice.webxml.com.cn/", key); element.setTextContent(value); SoapHeader header=new SoapHeader(qName, element); return(header); } }再给个有子节点的header例子:
public void handleMessage(SoapMessage arg0) throws Fault { QName qName=new QName("http://yourdomain.com/"); Document doc = DOMUtils.createDocument(); Element root = doc.createElement("AuthenticationToken"); Element username = doc.createElement("username"); username.setTextContent("yeetrack.com"); Element password = doc.createElement("password"); password.setTextContent("yeetrack.com"); Element args = doc.createElement("someargs"); args.setTextContent("其他参数"); root.appendChild(username); root.appendChild(password); root.appendChild(args); SoapHeader header = new SoapHeader(qName, root); // 获取SOAP消息的全部头 List<Header> headers = arg0.getHeaders(); headers.add(header); }然后,再我们的测试case中,添加拦截器:
//实例化接口实现类 MobileCodeWS mobileCodeWS = new MobileCodeWS(); //实例化接口 MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap(); Client client = ClientProxy.getClient(mobileCodeWSSoap); client.getOutInterceptors().add(new SoapHeaderInterceptor()); //调用接口中的方法 System.out.println(mobileCodeWSSoap.getMobileCodeInfo("13898767654", ""));依赖的jar包:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>org.apache.ws.security</groupId> <artifactId>wss4j</artifactId> <version>1.6.10</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>2.7.5</version> </dependency>效果如wireshark抓包:
![](http://static.oschina.net/uploads/img/201309/28005650_V74X.jpg)
maven cxf-codegen-plugin插件测试webservice
相关文章推荐
- 用apache-cxf生成webservice客户端的时候报错Parameter: shead already exists for method
- CXF WebService服务端与测试
- webservice快速入门-使用spring整合apache-cxf(五)
- webservice CXF整合spring服务端测试并生成wsdl文件
- 使用Maven 插件 cxf-codegen-plugin生成WebService测试程序
- 基于CXF和SSH框架的WebService发布,测试
- dubbo webservice 之java.lang.NoSuchMethodError: org.apache.cxf.transport.http.HTTPTransportFactory
- 【webservice】org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://webservice.index.
- WebService学习笔记-CXF添加拦截器
- 使用Apache自带ab工具测试WebService接口
- 使用apache-cxf-2.2.10来制作一个极简版WebService程序
- 使用cxf 发布 jax-rs 风格webservice 。并客户端测试。
- cxf动态调用webservice设置超时,测试线程安全
- 编写客户端测试WebService(CXF)
- CXF和Axis2开发webservice也是可以添加asmx等后缀
- Webservice:CXF添加拦截器实现权限控制
- WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)
- WebService学习笔记-CXF添加自定义拦截器
- webservice快速入门-整合apache-cxf(四)
- CXF开发webService客户端调用服务器端测试