1.增加Header頭
// 導(dǎo)包 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;
// 導(dǎo)包 end
List<Header> headers = new ArrayList<Header>();
Document document = DOMUtils.createDocument();
/**
* createElementNS 設(shè)置命名空間url 以及名稱
*/
Element messageId = document.createElementNS("nameSpace", "MessageID");
/**
* 設(shè)置值
*/
messageId.setTextContent(uuID);
/**
* 設(shè)置前綴
*/
messageId.setPrefix("wsa");
headers.add(new Header(new QName(""), messageId));
((BindingProvider) Myservice).getRequestContext().put(Header.HEADER_LIST, headers);
生成XML結(jié)果如下
<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.添加攔截器
/**
* 客戶端調(diào)用請(qǐng)求時(shí)超時(shí)設(shè)置
*
* @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 請(qǐng)求時(shí)間
policy.setReceiveTimeout(10 * 1000);//10S 連接時(shí)間
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() {
/**
* 必須在構(gòu)造中設(shè)置攔截階段 ?。。。。。。。。。? * 順序?yàn)閺纳系较?依次排序 具體見(jiàn) org.apache.cxf.phase.Phase
*/
super(Phase.PRE_STREAM_ENDING);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
//具體業(yè)務(wù)代碼啦~~~~~
}
}
3.soap:Envelope 添加命名空間文章來(lái)源:http://www.zghlxwxcb.cn/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結(jié)果如下文章來(lái)源地址http://www.zghlxwxcb.cn/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>
到了這里,關(guān)于WebService 客戶端增加Header頭、并且指定命名空間、添加攔截器(日志攔截器,自定義攔截器)、soap:Envelope 添加命名空間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!