1. 程式人生 > >CXF客戶端(動態呼叫)

CXF客戶端(動態呼叫)

所需包清單 含(服務端、客戶端):

cxf-core-3.1.8

cxf-rt-bindings-soap-3.1.8

cxf-rt-databinding-jaxb-3.1.8

cxf-rt-frontend-jaxws-3.1.8

cxf-rt-frontend-simple-3.1.8

cxf-rt-transports-http-3.1.8

cxf-rt-transports-udp-3.1.8

cxf-rt-ws-addr-3.1.8

cxf-rt-wsdl-3.1.8

cxf-rt-ws-policy-3.1.8

neethi-3.0.3

slf4j-api-1.7.21

xmlschema-core-2.2.1

動態方式-demo:

public class GetWebServiceData {
    private static final QName SERVICE_NAME = new QName(namespace, serviceName);
    /**
     * 
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // 遠端webService的URL
        String hostUrl = "****************?wsdl";
        try {
            // 建立動態客戶端
            JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
            // 建立客戶端連線
            Client client = factory.createClient(hostUrl, SERVICE_NAME);
            ClientImpl clientImpl = (ClientImpl) client;
            Endpoint endpoint = clientImpl.getEndpoint();
 
            ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0);
            // 建立QName來指定NameSpace和要呼叫的service(bingdingName)
            QName bindingName = new QName(namespace, service);
            BindingInfo binding = serviceInfo.getBinding(bindingName);
            // 建立QName來指定NameSpace和要呼叫的方法
            QName opName = new QName(namespace, methodName);
            BindingOperationInfo boi = binding.getOperation(opName);
            BindingMessageInfo inputMessageInfo = boi.getInput();
            List<MessagePartInfo> parts = inputMessageInfo.getMessageParts();
            // 取得物件例項
            MessagePartInfo partInfo = parts.get(0);
            Class<?> partClass = partInfo.getTypeClass();
            Object inputObject = partClass.newInstance();
 
   

          //------------------反射 設定 引數(如果深層物件 要繼續反射 往下)

          // 取得欄位的set方法並賦值 
               PropertyDescriptor partPropertyDescriptor = new PropertyDescriptor(欄位名,inputObject.getClass());
               Method userNoSetter = partPropertyDescriptor.getWriteMethod(); 
               userNoSetter.invoke(inputObject, 屬性值); 
          // 取得欄位的set方法並賦值 
              PropertyDescriptor partPropertyDescriptor2 = new PropertyDescriptor(欄位名, inputObject.getClass());
              Method productCodeSetter = partPropertyDescriptor2.getWriteMethod(); 
              productCodeSetter.invoke(inputObject, 屬性值); 
          //--------------------------------------------------------------- 
        
         // 呼叫客戶端invoke()方法,把inputObject傳遞給要呼叫的方法並取得結果物件 
          Object[] result = client.invoke(opName, inputObject); 
         // 取得的結果是一個物件 
          Class<?> resultClass = result[0].getClass();


        //------------------反射 獲取 物件 (如果深層物件 要繼續反射 往下)


       // 取得返回結果的get方法並得到它的值 
           PropertyDescriptor resultDescriptor = new PropertyDescriptor(結果欄位名, resultClass); 
           Object resultGetter = resultDescriptor.getReadMethod().invoke(result[0]);
          System.out.println("result:" + resultGetter);

           //-------------------------------------------------------------------- 
           } catch (Exception e) { 
             e.printStackTrace();
           } 
}