1.增加Header头
// 导包 begin
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
// 导包 end
List<Header> headers = new ArrayList<Header>();
Document document = DOMUtils.createDocument();
/**
* createElementNS 设置命名空间url 以及名称
*/
Element messageId = document.createElementNS("nameSpace", "MessageID");
/**
* 设置值
*/
messageId.setTextContent(uuID);
/**
* 设置前缀
*/
messageId.setPrefix("wsa");
headers.add(new Header(new QName(""), messageId));
((BindingProvider) Myservice).getRequestContext().put(Header.HEADER_LIST, headers);
生成XML结果如下
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
<wsa:MessageID
xmlns:wsa="namespace">34277dca-f9b8-4b30-be66-77d8841a7532
</wsa:MessageID>
</soap:Header>
<soap:Body>
。。。。。。。
</soap:Body>
</soap:Envelope>
2.添加拦截器
/**
* 客户端调用请求时超时设置
*
* @param service 为 客户端的interface
*/
public static void configTimeout(Object service) {
Client proxy = ClientProxy.getClient(service);
/**
* 日志拦截器
*/
proxy.getInInterceptors().add(new LoggingInInterceptor());
proxy.getOutInterceptors().add(new LoggingOutInterceptor());
/**
* 自定义拦截器
*/
proxy.getOutInterceptors().add(new MyClientIntecepter);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(8 * 1000);//8S 请求时间
policy.setReceiveTimeout(10 * 1000);//10S 连接时间
conduit.setClient(policy);
}
/**
*自定义拦截器
*/
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class MyClientIntecepter extends AbstractPhaseInterceptor<SoapMessage> {
public MyClientIntecepter() {
/**
* 必须在构造中设置拦截阶段 !!!!!!!!!!
* 顺序为从上到下 依次排序 具体见 org.apache.cxf.phase.Phase
*/
super(Phase.PRE_STREAM_ENDING);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
//具体业务代码啦~~~~~
}
}
3.soap:Envelope 添加命名空间文章来源:https://www.toymoban.com/news/detail-686786.html
Map<String, String> nsMap = new HashMap<String, String>();
/**
* key 为命名空间前缀 value 为地址
*/
nsMap.put("wsa","http://www.w3.org/2005/08/addressing");
/**
* Myservice 就是你自己的接口了
*/
Map<String, Object> requestContext = ((BindingProvider) Myservice).getRequestContext();
requestContext.put("soap.env.ns.map", nsMap);
生成XML结果如下文章来源地址https://www.toymoban.com/news/detail-686786.html
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
</soap:Body>
</soap:Envelope>
到了这里,关于WebService 客户端增加Header头、并且指定命名空间、添加拦截器(日志拦截器,自定义拦截器)、soap:Envelope 添加命名空间的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!