JAVA动态调用Web Services

服务端的webservices的发布是基于jax-ws规范发布的。下面是客户端动态调用的代码,调用的web服务是可配置的。

 

1:在xml文档里配置webservices的wsdl地址,端口名和操作名

2:提供一个方法读取xml文档,返回一个List

 

     List list= ReadAndWriteXML.getWebServiceWsdlList();
     if(list!=null){
      String responseContent ="";
           for(int i=0;i<list.size();i++){
            Address address = (Address)list.get(i);
            try {  
            // Define the service.
                     QName svcQname = new QName(
                             address.getTargetNameSpace(),
                             address.getServiceName());
                     QName portQName = new QName(
                       address.getTargetNameSpace(),
                             address.getPortName());
                     Service svc = Service.create(svcQname);
                     svc.addPort(
                             portQName,  
                             SOAPBinding.SOAP11HTTP_BINDING,
                             address.getSoapAddress());

                     // Create the dynamic invocation object from this service.
                     Dispatch<Source> dispatch = svc.createDispatch(
                             portQName,
                             Source.class,
                             Service.Mode.PAYLOAD);

                     // Build the message.
         
                        String content =
                      "<?xml version=/"1.0/" encoding=/"gb2312/"?>"+
                       "<soap:"+address.getOperationName()+" xmlns:soap=/""+address.getTargetNameSpace()+"/" "+
                        "soap:encodingStyle=/"http://www.w3.org/2001/12/soap-encoding/">"+
                             "<fullFormerName>"+fullFormerName+"</fullFormerName>"+
                             "<fullLaterName>"+fullLaterName+"</fullLaterName>"+
                        "</soap:"+address.getOperationName()+">";                      
                     ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
                     Source input = new StreamSource(bais);
                     // Invoke the operation.
                     Source output = dispatch.invoke(input);
                     // Process the response.
                     StreamResult result = new StreamResult(new ByteArrayOutputStream());
                     Transformer trans;
            
             trans = TransformerFactory.newInstance().newTransformer();
             trans.transform(output, result);
              ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();

                     // Write out the response content.
                       responseContent = new String(baos.toByteArray());
            } catch (TransformerConfigurationException e) {  
             System.out.println("NounConsistencyAcceptor 92 Line TransformerConfigurationException ...");
            } catch (TransformerFactoryConfigurationError e) {
              System.out.println("NounConsistencyAcceptor 94 Line TransformerFactoryConfigurationError ...");
            }  catch (TransformerException e) {
              System.out.println("NounConsistencyAcceptor 96 Line TransformerException ...");
            }
                    
                           
            }
 
     }

   

xml配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
 <!-- 配置说明:targetNameSpace为wsdl文档中的targetNameSpace -->
  <!-- serviceName为wsdl文档中的service片断 name名 -->
   <!--portName为wsdl文档中的service片断 port name名 -->
    <!--soapAddress为wsdl文档中的service片断 soap:address location地址 -->
<Message> 
 
     <address>
       <targetNameSpace>http://service.zf.com/</targetNameSpace>
       <serviceName>FileConsistServiceService</serviceName>
       <portName>FileConsistServicePort</portName>
       <operationName>consistDesignNodeName</operationName>
       <soapAddress>http://127.0.0.1:8080/KMEntity/FileConsistService</soapAddress>
    </address>  
     <address>
       <targetNameSpace>http://service.zf.com/</targetNameSpace>
       <serviceName>FileConsistServiceService</serviceName>
       <portName>FileConsistServicePort</portName>
       <operationName>consistDesignParaName</operationName>
       <soapAddress>http://127.0.0.1:8080/KMEntity/FileConsistService</soapAddress>
    </address>    
 
      <address>
       <targetNameSpace>http://service.zf.com/</targetNameSpace>
       <serviceName>IndexConsistServiceService</serviceName>
       <portName>IndexConsistServicePort</portName>
       <operationName>consistDesignNodeName</operationName>
       <soapAddress>http://127.0.0.1:8080/KMEntity/IndexConsistService</soapAddress>
    </address>  
     <address>
       <targetNameSpace>http://service.zf.com/</targetNameSpace>
       <serviceName>IndexConsistServiceService</serviceName>
       <portName>IndexConsistServicePort</portName>
       <operationName>consistDesignParaName</operationName>
       <soapAddress>http://127.0.0.1:8080/KMEntity/IndexConsistService</soapAddress>
    </address>
</Message>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值