本站不再更新,歡迎光臨 java開發(fā)技術(shù)網(wǎng)
          隨筆-230  評論-230  文章-8  trackbacks-0
          axis常見問題及解決辦法參考這里
          http://www.ibm.com/developerworks/cn/webservices/ws-axisfaq/

          根據(jù)wsdl開成webservice的java客戶 例子:
          1、執(zhí)行以下命令生成客戶端代碼
          Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/Hello.jws?wsdl  
          該命令的參數(shù)格式
          從最簡單的開始 ,-uri 指定wsdl文件
          > WSDL2Java -uri currencyConvert.wsdl

          -d 使用不同的data binding方法
          > WSDL2Java -uri currencyConvert.wsdl -d xmlbeans

          -a 生成異步的方法
          > WSDL2Java -uri currencyConvert.wsdl -a

          -t 生成測試case
          > WSDL2Java -uri currencyConvert.wsdl -t 

          稍微復(fù)雜一些的,-p可以指定生成的package,-o指定生成的路徑,-ss生成服務(wù)端代碼
          wsdl2java -uri ../wsdl/currencyConvert.wsdl -o ../gen_src  -ss -sd -g -p foo.bat


          2、生成代碼的使用
          /* 
          * Created on 2004-1-17 

          * TODO To change the template for this generated file go to 
          * Window - Preferences - Java - Code Style - Code Templates 
          */ 
          package localhost.axis.pdw_jws; 

          import java.rmi.RemoteException; 

          import javax.xml.rpc.ServiceException; 

          /** 
          @author Administrator 

          * TODO To change the template for this generated type comment go to 
          * Window - Preferences - Java - Code Style - Code Templates 
          */ 
          public class clientTest { 

          public static void main(String[] args) throws ServiceException, RemoteException { 
          PdwService service
          =new PdwServiceLocator(); 
          Pdw p
          =service.getpdw(); 
          int count=p.add(3,4); 
          System.out.println(count); 
          System.out.println(p.sub(
          4,6)); 


          開發(fā)環(huán)境地的建立和以往一樣,把例子中的classes的文件打包成jar,放到開發(fā)環(huán)境,就可能了。

          這是我3年前寫的,在myeclipse中進行axis開發(fā)的文件
          http://www.54bk.com/user1/6324/archives/2005/22197.html


          最近復(fù)習(xí)寫的代碼
          server-config.xml 代碼如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
           
          <globalConfiguration>
            
          <parameter name="adminPassword" value="admin"/>
            
          <parameter name="attachments.Directory" value="D:\resin-pro-3.1.0\webapps\axis\WEB-INF\attachments"/>
            
          <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
            
          <parameter name="sendXsiTypes" value="true"/>
            
          <parameter name="sendMultiRefs" value="true"/>
            
          <parameter name="sendXMLDeclaration" value="true"/>
            
          <parameter name="axis.sendMinimizedElements" value="true"/>
            
          <requestFlow>
             
          <handler type="java:org.apache.axis.handlers.JWSHandler">
              
          <parameter name="scope" value="session"/>
             
          </handler>
             
          <handler type="java:org.apache.axis.handlers.JWSHandler">
              
          <parameter name="scope" value="request"/>
              
          <parameter name="extension" value=".jwr"/>
             
          </handler>
            
          </requestFlow>
           
          </globalConfiguration>
           
          <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
           
          <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
           
          <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>

           
           
          <transport name="local">
            
          <responseFlow>
             
          <handler type="LocalResponder"/>
            
          </responseFlow>
           
          </transport>
           
          <transport name="http">
            
          <requestFlow>
             
          <handler type="URLMapper"/>
             
          <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
            
          </requestFlow>
           
          </transport>
           
           
          <handler name="soapmonitor" 
              type
          ="java:org.apache.axis.handlers.SOAPMonitorHandler"> 
              
          <parameter name="wsdlURL" 
                value
          ="/axis/SOAPMonitorService-impl.wsdl"/> 
              
          <parameter name="namespace" 
                value
          ="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/> 
              
          <parameter name="serviceName" value="SOAPMonitorService"/> 
              
          <parameter name="portName" value="Demo"/> 
           
          </handler> 
           
          <!-- 日志handler -->
           
          <handler name="logHandler" type="java:webservices.handlers.LogHandler">
               
          <parameter name="filename" value="c:\\axislog.txt"/>
           
          </handler>
           
          <!-- 認(rèn)證handler -->
           
          <handler name="authorHandler" type="java:webservices.handlers.AuthenticationHandler"/>
            
            
          <service name="SOAPMonitorService" provider="java:RPC"> 
              
          <parameter name="allowedMethods" value="publishMessage"/> 
              
          <parameter name="className" 
                value
          ="org.apache.axis.monitor.SOAPMonitorService"/> 
              
          <parameter name="scope" value="Application"/> 
            
          </service> 
            
            
          <service name="Version" provider="java:RPC">
                
          <parameter name="allowedMethods" value="getVersion"/>
                
          <parameter name="className" value="org.apache.axis.Version"/>
            
          </service>

            
          <service name="AdminService" provider="java:MSG">
               
          <parameter name="allowedMethods" value="AdminService"/>
               
          <parameter name="enableRemoteAdmin" value="false"/>
               
          <parameter name="className" value="org.apache.axis.utils.Admin"/>
               
          <namespace>http://xml.apache.org/axis/wsdd/</namespace>
            
          </service>
            
            
          <service name="MyServices" provider="java:RPC">
                
          <parameter name="allowedMethods" value="*"/>
                
          <parameter name="allowedRoles" value="peidw"/><!-- 驗證規(guī)則 -->
                
          <parameter name="className" value="webservices.MyServices"/>
                
          <beanMapping languageSpecificType="java:domain.Book" qname="ns:Book" xmlns:ns="urn:BeanService" />
                 
          <beanMapping languageSpecificType="java:domain.Student" qname="ns:Student" xmlns:ns="urn:StudentService" />
                 
                
          <requestFlow>
                  
          <handler type="logHandler"/>
              
                
          </requestFlow>
            
          </service>
            
           
          </deployment>
          myservice.java
          package webservices;

          import domain.Book;
          import domain.Student;

          import java.util.*;
          /**
           * WebService 服務(wù)類(測試省略了接口)
           * 
          @author peidw
           *
           
          */
          public class MyServices {
             
              
          static Map map=new HashMap();
              
          static{
                  map.put(
          "125-6922-10"new Book("Structs程序設(shè)計","孫衛(wèi)琴","125-6922-10"));
                  map.put(
          "125-6922-11"new Book("Think in Java","孫衛(wèi)琴","125-6922-11"));
                  map.put(
          "125-6922-12"new Book("C++程序高駐地","小張","125-6922-12"));
                  map.put(
          "125-6912-59"new Book("CSS實踐手冊","小李","125-6912-59"));
                  map.put(
          "125-6992-55"new Book("XML入門到精通","小裴","125-6992-55"));
              }
              
          /**
               * 返回對象類型
               * 
          @param isbn
               * 
          @return
               
          */
              
          public Book getBookByIsbn(String isbn){
                  Book result
          =null;
                  
          if(map.containsKey(isbn)){
                      
          return (Book) map.get(isbn);
                  }
                  
          return result;
              }
              
          /**
               * 返回一個list類型
               * 
          @return
               
          */
              
          public List getBookList(){
                  List result
          =null;
                  Set set
          =map.entrySet();
                  result
          =new ArrayList();
                  result.addAll(set);
                  
          return result;
              }
              
          /**
               * 返回對象數(shù)組
               * 
          @return
               
          */
              
          public Book[] getBookArray(){
                  List tmp_list
          =getBookList();
                  
          return (Book[]) tmp_list.toArray();
              }
              
          /**
               * 返回一個Map
               * 
          @return
               
          */
              
          public  Map getAllBookMap(){
                  
          return map;
              }
              
          /**
               * 返回對象測試
               * 
          @return
               
          */
              
          public Student getStudent(){
                  
          return new Student("小裴","kkk@tom.com","廣西合浦西場裴屋村");
              }
              
              
          }

          兩個handler
          package webservices.handlers;

          import org.apache.axis.AxisFault;
          import org.apache.axis.Handler;
          import org.apache.axis.MessageContext;
          import org.apache.axis.handlers.BasicHandler;
          import org.apache.commons.lang.*;
          import java.io.*;
          import java.util.*;

          /**
           * 日志handler ,handler在axis的地位,相當(dāng)于servlet的filter!可以在所有的service中添加多個handler
           * 
          @author KT4600
           *
           
          */
          public class LogHandler extends BasicHandler{

              
          public void invoke(MessageContext arg0) throws AxisFault {
                  Handler handler
          =arg0.getService();
                  String logfilename
          =(String)this.getOption("filename");
                  
          if(StringUtils.isEmpty(logfilename)){
                      
          throw new AxisFault("日志文件不能為空","",null,null);
                  }
                  
          try {
                      FileOutputStream fos
          =new FileOutputStream(logfilename,true);
                      PrintWriter pw
          =new PrintWriter(fos);
                      Date date
          =new Date();
                      arg0.getMessage().writeTo(System.out);
                      String result
          ="---";
                      pw.println(result);
                      pw.close();
                  } 
          catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
              
              
          }

          package webservices.handlers;

          import org.apache.axis.AxisFault;
          import org.apache.axis.MessageContext;
          import org.apache.axis.handlers.BasicHandler;
          import org.apache.axis.security.SecurityProvider;
          import org.apache.axis.security.simple.SimpleSecurityProvider;
          import org.apache.axis.session.Session;

          /**
           * 自己寫的認(rèn)證handler,也可以使用axis自帶的,只需配置在server-config.wsdd中添加
           * <parameter name="allowedRoles" value="ronghao"/>   證證規(guī)則即可
           * 
          @author peidw
           *
           
          */
          public class AuthenticationHandler extends BasicHandler{

              
          public void invoke(MessageContext arg0) throws AxisFault {
                  SecurityProvider provider 
          = (SecurityProvider)arg0.getProperty("securityProvider");
                  
          if(provider==null){
                      provider
          = new SimpleSecurityProvider();
                      arg0.setProperty(
          "securityProvider", provider);
                  }
                  
          if(provider!=null){
                      String userId
          =arg0.getUsername();
                      String password
          =arg0.getPassword();
                      
          //對用戶進行認(rèn)證,如果authUser==null,表示沒有通過認(rèn)證,拋出Server.Unauthenticated異常。
                      Session session=arg0.getSession();
                      System.out.println(
          "----------===xx----------------");
                      org.apache.axis.security.AuthenticatedUser authUser
                      
          = provider.authenticate(arg0);
                      
          if(authUser==null)    
                          
          throw new AxisFault("Server.Unauthenticated","用戶驗證異常"null,null);
                      
          //用戶通過認(rèn)證,把用戶的設(shè)置成認(rèn)證了的用戶。
                      arg0.setProperty("authenticatedUser", authUser);
                  }
                  
              }
              
          }

          客戶端例子
          package test;

          import wsclient.myservices.*;

          import java.net.URL;
          import java.util.*;
          import javax.xml.namespace.QName;
          import org.apache.axis.client.*;

          import org.apache.wsif.*;
          import org.apache.wsif.wsdl.AuthenticatingProxyWSDLLocatorImpl;

          /**
           * axis1.x客戶端調(diào)用測試
           * 
          @author peidw
           *
           
          */
          public class MyServiceExe {
              
          /**
               * 使用axis的WSDL2JAVA調(diào)用WS服務(wù),如果服務(wù)器端添加了簡單handler用戶驗證,則需要修改
               * ..SoapBindingStub類的createCall()方法,
               * 
          @throws Exception
               
          */
              
          public static  void handlerCallDemo()throws Exception{
                  MyServicesService ms
          =new MyServicesServiceLocator();
                  MyServices_PortType msp
          =ms.getMyServices();
                  Book book
          =msp.getBookByIsbn("125-6922-10");
                  System.out.println(book.getAuthor()
          +"-"+book.getBookname()+"-"+book.getIsbn());
                  Map map
          =msp.getAllBookMap();
                  Set keyset
          =map.keySet();
                  Iterator it
          =keyset.iterator();
                  String tmp
          =null;
                  
          while(it.hasNext()){
                      tmp
          =(String)it.next();
                      System.out.println(tmp);
                  }
              }
              
          /**
               * 通過動態(tài)接口方式調(diào)用,使用axis提供的client包
               * 最簡單的使用方式
               * 
          @throws Exception
               
          */
              
          public static void dynamicCall()throws Exception{
                  String endpoint_1
          ="http://localhost:8000/axis/services/Version?wsdl";
                  Service service 
          = new Service(); 
                  Call call
          =(Call)service.createCall();
                  call.setOperationName(
          new QName(endpoint_1,"getVersion "));
                  call.setTargetEndpointAddress(
          new URL(endpoint_1));
                  String result
          =(String)call.invoke(new Object[]{});
                  System.out.println(
          "result="+result);
              }
              
          /**
               * 使用WSIF來調(diào)用axis發(fā)布的服務(wù)
               * <pre>
               * WSIF具有可對任何 Web 服務(wù)進行獨立于綁定的訪問的 API。
               * 它緊密依賴于 WSDL,因此可以調(diào)用可在 WSDL 中描述的任何服務(wù)。 
               * </pre>
               * 
          @throws Exception
               
          */
              
          public static void wsifCall()throws Exception {
                  String url
          ="http://localhost:8000/axis/services/Version?wsdl";
                  WSIFServiceFactory factory
          =WSIFServiceFactory.newInstance();
                  
          //如果調(diào)用的方法需要進行用戶/密碼校驗,需執(zhí)行下面代碼
                  
          //AuthenticatingProxyWSDLLocatorImpl awsli=new AuthenticatingProxyWSDLLocatorImpl(url,"gaolong1","19831001"); //驗證連接
                  WSIFService service=factory.getService(url,"http://localhost:8000/axis/services/Version","VersionService","http://localhost:8000/axis/services/Version","Version");
                  WSIFPort port 
          = service.getPort();
                  WSIFOperation operation 
          = port.createOperation("getVersion","getVersionRequest",null);//根據(jù)給定的操作名稱參數(shù)operationName,輸入元素名稱inputName,輸出元素名稱
                  WSIFMessage input = operation.createOutputMessage();//設(shè)置輸入?yún)?shù)
                  WSIFMessage output = operation.createOutputMessage();//設(shè)置輸出參數(shù)
                  WSIFMessage fault = operation.createFaultMessage(); //異常信息
                  operation.executeRequestResponseOperation(input,output,fault); //執(zhí)行請求
                  
                  System.out.println(output.getObjectPart(
          "getVersionReturn"));
                  
              }
              
          public static void main(String[] args)throws Exception{
                  handlerCallDemo();
                  dynamicCall();
                  wsifCall();
              }
          }
          posted on 2007-10-25 16:25 有貓相伴的日子 閱讀(3537) 評論(0)  編輯  收藏 所屬分類: webservices
          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(32)

          隨筆分類(222)

          隨筆檔案(224)

          文章分類(4)

          文章檔案(7)

          相冊

          收藏夾(4)

          最新評論

          閱讀排行榜

          評論排行榜

          本站不再更新,歡迎光臨 java開發(fā)技術(shù)網(wǎng)
          主站蜘蛛池模板: 白城市| 云林县| 永和县| 叙永县| 铜梁县| 临安市| 四会市| 积石山| 万荣县| 兰考县| 彭州市| 犍为县| 涟水县| 广灵县| 深州市| 乌恰县| 青岛市| 太仆寺旗| 万宁市| 潼关县| 武宣县| 武胜县| 修武县| 通道| 澄城县| 昌邑市| 清河县| 星座| 北海市| 汾西县| 广宁县| 奎屯市| 山东省| 安西县| 安福县| 巴东县| 探索| 延庆县| 枣强县| 衡东县| 沾化县|