乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      利用JAX-WS開發(fā)Web服務(wù) - 架構(gòu)與設(shè)計(jì) -

       汲取者 2010-04-25

      本文提供了一個(gè)使用Java如何開發(fā)基于SOAP的Web Services,其客戶端可以是Perl、Ruby、Python或Java等。

      Java SE 6封裝了JAX-WS(Java API for XML-Web Services),而JAX-WS同時(shí)支持基于SOAP的Web服務(wù)和REST風(fēng)格的Web服務(wù)。JAX-WS通??珊?jiǎn)寫為JWS,當(dāng)前,JWS的版本 為2.x。

      基于SOAP的Web服務(wù)可用單個(gè)Java類的實(shí)現(xiàn),但是最好是用“接口+實(shí)現(xiàn)”的方式來實(shí)現(xiàn)最佳。


      Web服務(wù)的接口稱為SEI,即Service Endpoint Interface;
      而Web服務(wù)的實(shí)現(xiàn)稱為SIB,即 Service Implementation Bean。

      SIB可以是一個(gè)POJO,也可以是無狀態(tài)的會(huì)話EJB。本文的SIB是普通Java類,通過JDK 6的類庫即可實(shí)現(xiàn)Web服務(wù)的發(fā)布。

      代碼1:服務(wù)接口類SEI

      1. package myweb.service;  
      2. import javax.jws.WebService;  
      3. import javax.jws.WebMethod;  
      4. import javax.jws.soap.SOAPBinding;  
      5. import javax.jws.soap.SOAPBinding.Style;  
      6. @WebService  
      7. @SOAPBinding(style=Style.RPC)  
      8. public interface TimeServer {  
      9.     @WebMethod  
      10.     String getTimeAsString();  
      11.     @WebMethod  
      12.     long getTimeAsElapsed();  
      13. }  

      代碼2:服務(wù)實(shí)現(xiàn)類SIB

      1. package myweb.service;  
      2. import java.text.DateFormat;  
      3. import java.util.Date;  
      4. import javax.jws.WebService;  
      5.   
      6. @WebService(endpointInterface = "myweb.service.TimeServer")  
      7. public class TimeServerImpl implements TimeServer {  
      8.   
      9.     /** 
      10.      * 返回從1970年1月1日0點(diǎn)0時(shí)0分起的毫秒數(shù) 
      11.      */  
      12.     public long getTimeAsElapsed() {  
      13.         return new Date().getTime();  
      14.     }  
      15.   
      16.     /** 
      17.      * 返回如“2009-12-21”格式的日期 
      18.      */  
      19.     public String getTimeAsString() {  
      20.         Date date = new Date();  
      21.         DateFormat df = DateFormat.getDateInstance();  
      22.         return df.format(date);  
      23.     }  
      24. }  

      代碼3:服務(wù)發(fā)布類Publisher

      1. package myweb.service;  
      2. import javax.xml.ws.Endpoint;  
      3.   
      4. public class TimeServerPublisher {  
      5.     public static void main(String[] args){  
      6.         // 第一個(gè)參數(shù)是發(fā)布的URL  
      7.         // 第二個(gè)參數(shù)是SIB實(shí)現(xiàn)  
      8.         Endpoint.publish("http://127.0.0.1:10100/myweb"new TimeServerImpl());  
      9.     }  
      10. }  

      編譯以上代碼:

      javac myweb/service/*.java

      運(yùn)行服務(wù):

      java myweb/service/TimeServerPublisher

      在瀏覽器地址欄輸入:

      http://localhost:10100/myweb?wsdl

      顯示如下圖所示:

      服務(wù)內(nèi)容

      也可編寫客戶端代碼測(cè)試服務(wù)。

      Java客戶端:

      1. package myweb.client;  
      2. import javax.xml.namespace.QName;  
      3. import javax.xml.ws.Service;  
      4. import java.net.URL;  
      5. import myweb.service.*;  
      6. public class TimeClient {  
      7.     public static void main(String[] args) throws Exception{  
      8.         URL url = new URL("http://localhost:10100/myweb?wsdl");  
      9.         // 第一個(gè)參數(shù)是服務(wù)的URI  
      10.         // 第二個(gè)參數(shù)是在WSDL發(fā)布的服務(wù)名  
      11.         QName qname = new QName("http://web/","TimeServerImplService");  
      12.         // 創(chuàng)建服務(wù)  
      13.         Service service = Service.create(url, qname);  
      14.         // 提取端點(diǎn)接口,服務(wù)“端口”。  
      15.         TimeServer eif = service.getPort(TimeServer.class);  
      16.         System.out.println(eif.getTimeAsString());  
      17.         System.out.println(eif.getTimeAsElapsed());  
      18.     }  
      19. }  

      運(yùn)行客戶端,顯示結(jié)果如下:

      2009-12-21
      1261402511859

      也可用Ruby編寫客戶端,如下:

      1. #!/usr/bin/ruby  
      2.   
      3. # one Ruby package for SOAP-based services  
      4. require 'soap/wsdlDriver'   
      5.   
      6. wsdl_url = 'http://127.0.0.1:10100/myweb?wsdl'  
      7.   
      8.   
      9. service = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver  
      10.   
      11. # Save request/response messages in files named '...soapmsgs...'  
      12. service.wiredump_file_base = 'soapmsgs'  
      13.   
      14. # Invoke service operations.  
      15. result1 = service.getTimeAsString  
      16. result2 = service.getTimeAsElapsed  
      17.   
      18. # Output results.  
      19. puts "Current time is: #{result1}"  
      20. puts "Elapsed milliseconds from the epoch: #{result2}"  

      運(yùn)行結(jié)果相同!

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多