---
title: "CXF进行WebService客户端调用的两种方式"
author: "LD_21"
date: "2016-11-22T13:33:59Z"
tags: []
categories: ["编程语言"]
slug: "cxf-webservice"
draft: false
lastmod: "2016-11-22T13:38:25Z"
id: 2339756
url: "https://ld-21.iteye.com/blog/2339756"
---

1、JaxWsDynamicClientFactory：

```
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();  
Client client = clientFactory.createClient("http://IP:端口/iemr/webservice/IEMRWebService?wsdl");  
// 下面一段处理 WebService接口和实现类namespace不同的情况  
// CXF动态客户端在处理此问题时，会报No operation was found with the name的异常  
String wsOperation="getMrPdfList";//ws调用方法
Endpoint endpoint = client.getEndpoint();  
QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), wsOperation);  
BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();  
if (bindingInfo.getOperation(opName) == null) {  
 for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {  
	 if (wsOperation.equals(operationInfo.getName().getLocalPart())) {  
		 opName = operationInfo.getName();  
		 break;  
	 }  
 }  
} 
Object[] result = client.invoke(opName, enPk, true, false);  
String xmlReturn=(String)result[0]; 
```

 

2、JaxWsProxyFactoryBean：

```
private UnDocPdfWebService createUnDocPdfWs(){
	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    //带有@WebService注解的接口类
	factory.setServiceClass(UnDocPdfWebService.class);
    //不带?wsdl的WS调用地址
	factory.setAddress(unDocPdfWsUrl);
	UnDocPdfWebService service = (UnDocPdfWebService)factory.create();
	return service;
}
```