apache-cxf测试webservice添加header信息

使用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抓包:
wireshark抓取webservice
maven cxf-codegen-plugin插件测试webservice

One thought on “apache-cxf测试webservice添加header信息

发表评论

电子邮件地址不会被公开。 必填项已用*标注

(Spamcheck Enabled)