亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 43800 | 回復(fù): 1
打印 上一主題 下一主題

利用JAX-WS開(kāi)發(fā)Web服務(wù) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-12-23 10:17 |只看該作者 |倒序?yàn)g覽

本文提供了一個(gè)使用Java如何開(kāi)發(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)寫(xiě)為JWS,當(dāng)前,JWS的版本為2.x。
基于SOAP的Web服務(wù)可用單個(gè)Java類的實(shí)現(xiàn),但是最好是用“接口+實(shí)現(xiàn)”的方式來(lái)實(shí)現(xiàn)最佳。
Web服務(wù)的接口稱為SEI,即Service Endpoint Interface;
而Web服務(wù)的實(shí)現(xiàn)稱為SIB,即Service Implementation Bean。
SIB可以是一個(gè)POJO,也可以是無(wú)狀態(tài)的會(huì)話EJB。本文的SIB是普通Java類,通過(guò)JDK 6的類庫(kù)即可實(shí)現(xiàn)Web服務(wù)的發(fā)布。
代碼1:服務(wù)接口類SEI
view plain
copy to clipboard
print
?
package myweb.service;  import javax.jws.WebService;  import javax.jws.WebMethod;  import javax.jws.soap.SOAPBinding;  import javax.jws.soap.SOAPBinding.Style;  @WebService  @SOAPBinding(style=Style.RPC)  public interface TimeServer {      @WebMethod      String getTimeAsString();      @WebMethod      long getTimeAsElapsed();  }  package myweb.service;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style=Style.RPC)
public interface TimeServer {
        @WebMethod
        String getTimeAsString();
        @WebMethod
        long getTimeAsElapsed();
}
代碼2:服務(wù)實(shí)現(xiàn)類SIB
view plain
copy to clipboard
print
?
package myweb.service;  import java.text.DateFormat;  import java.util.Date;  import javax.jws.WebService;    @WebService(endpointInterface = "myweb.service.TimeServer")  public class TimeServerImpl implements TimeServer {        /**      * 返回從1970年1月1日0點(diǎn)0時(shí)0分起的毫秒數(shù)      */      public long getTimeAsElapsed() {          return new Date().getTime();      }        /**      * 返回如“2009-12-21”格式的日期      */      public String getTimeAsString() {          Date date = new Date();          DateFormat df = DateFormat.getDateInstance();          return df.format(date);      }  }  package myweb.service;
import java.text.DateFormat;
import java.util.Date;
import javax.jws.WebService;
@WebService(endpointInterface = "myweb.service.TimeServer")
public class TimeServerImpl implements TimeServer {
        /**
         * 返回從1970年1月1日0點(diǎn)0時(shí)0分起的毫秒數(shù)
         */
        public long getTimeAsElapsed() {
                return new Date().getTime();
        }
        /**
         * 返回如“2009-12-21”格式的日期
         */
        public String getTimeAsString() {
                Date date = new Date();
                DateFormat df = DateFormat.getDateInstance();
                return df.format(date);
        }
}
代碼3:服務(wù)發(fā)布類Publisher
view plain
copy to clipboard
print
?
package myweb.service;  import javax.xml.ws.Endpoint;    public class TimeServerPublisher {      public static void main(String[] args){          // 第一個(gè)參數(shù)是發(fā)布的URL          // 第二個(gè)參數(shù)是SIB實(shí)現(xiàn)          Endpoint.publish("http://127.0.0.1:10100/myweb", new TimeServerImpl());      }  }  package myweb.service;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher {
        public static void main(String[] args){
                // 第一個(gè)參數(shù)是發(fā)布的URL
                // 第二個(gè)參數(shù)是SIB實(shí)現(xiàn)
                Endpoint.publish("http://127.0.0.1:10100/myweb", new TimeServerImpl());
        }
}

編譯以上代碼:
javac myweb/service/*.java
運(yùn)行服務(wù):
java myweb/service/TimeServerPublisher
在瀏覽器地址欄輸入:
http://localhost:10100/myweb?wsdl
顯示如下圖所示:

也可編寫(xiě)客戶端代碼測(cè)試服務(wù)。
Java客戶端:
view plain
copy to clipboard
print
?
package myweb.client;  import javax.xml.namespace.QName;  import javax.xml.ws.Service;  import java.net.URL;  import myweb.service.*;  public class TimeClient {      public static void main(String[] args) throws Exception{          URL url = new URL("http://localhost:10100/myweb?wsdl");          // 第一個(gè)參數(shù)是服務(wù)的URI          // 第二個(gè)參數(shù)是在WSDL發(fā)布的服務(wù)名          QName qname = new QName("http://service.myweb/","TimeServerImplService");          // 創(chuàng)建服務(wù)          Service service = Service.create(url, qname);          // 提取端點(diǎn)接口,服務(wù)“端口”。          TimeServer eif = service.getPort(TimeServer.class);          System.out.println(eif.getTimeAsString());          System.out.println(eif.getTimeAsElapsed());      }  }  package myweb.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
import myweb.service.*;
public class TimeClient {
        public static void main(String[] args) throws Exception{
                URL url = new URL("http://localhost:10100/myweb?wsdl");
                // 第一個(gè)參數(shù)是服務(wù)的URI
                // 第二個(gè)參數(shù)是在WSDL發(fā)布的服務(wù)名
                QName qname = new QName("http://service.myweb/","TimeServerImplService");
                // 創(chuàng)建服務(wù)
                Service service = Service.create(url, qname);
                // 提取端點(diǎn)接口,服務(wù)“端口”。
                TimeServer eif = service.getPort(TimeServer.class);
                System.out.println(eif.getTimeAsString());
                System.out.println(eif.getTimeAsElapsed());
        }
}
運(yùn)行客戶端,顯示結(jié)果如下:
2009-12-21
1261402511859

也可用Ruby編寫(xiě)客戶端,如下:
view plain
copy to clipboard
print
?
#!/usr/bin/ruby    # one Ruby package for SOAP-based services  require 'soap/wsdlDriver'     wsdl_url = 'http://127.0.0.1:10100/myweb?wsdl'      service = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver    # Save request/response messages in files named '...soapmsgs...'  service.wiredump_file_base = 'soapmsgs'    # Invoke service operations.  result1 = service.getTimeAsString  result2 = service.getTimeAsElapsed    # Output results.  puts "Current time is: #{result1}"  puts "Elapsed milliseconds from the epoch: #{result2}"  #!/usr/bin/ruby
# one Ruby package for SOAP-based services
require 'soap/wsdlDriver'
wsdl_url = 'http://127.0.0.1:10100/myweb?wsdl'
service = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver
# Save request/response messages in files named '...soapmsgs...'
service.wiredump_file_base = 'soapmsgs'
# Invoke service operations.
result1 = service.getTimeAsString
result2 = service.getTimeAsElapsed
# Output results.
puts "Current time is: #{result1}"
puts "Elapsed milliseconds from the epoch: #{result2}"
運(yùn)行結(jié)果相同!
               
               
               

本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u2/88379/showart_2128563.html

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-08-25 18:56 |只看該作者
也可用Ruby編寫(xiě)客戶端
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP