使用apache-cxf自动生成webservice本地类,有时需要在soap中添加header信息,如下面
<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抓包:
maven cxf-codegen-plugin插件测试webservice
版权声明
本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。
© 空空博客,本文链接:https://www.yeetrack.com/?p=581
test comment