1. 程式人生 > >cxf動態呼叫webservice介面

cxf動態呼叫webservice介面

package cxfClient;

import org.apache.cxf.endpoint.Endpoint;
import javax.xml.namespace.QName;  
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;

public class CxfClient {

	public static void main(String[] args) throws Exception {
		String url = "http://localhost:9091/Service/SayHello?wsdl";
		String method = "say";
		Object[] parameters = new Object[]{"我是引數"};
		System.out.println(invokeRemoteMethod(url, method, parameters)[0]);
    }
	
	public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        if (!url.endsWith("wsdl")) {
            url += "?wsdl";
        }
        org.apache.cxf.endpoint.Client client = dcf.createClient(url);
        //處理webService介面和實現類namespace不同的情況,CXF動態客戶端在處理此問題時,會報No operation was found with the name的異常
        Endpoint endpoint = client.getEndpoint();
        QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation);
        BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding();
        if(bindingInfo.getOperation(opName) == null){
        	for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){
        		if(operation.equals(operationInfo.getName().getLocalPart())){
        			opName = operationInfo.getName();
        			break;
        		}
        	}
        }
		Object[] res = null;
		try {
			res = client.invoke(opName, parameters);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
    } 	
}