- 論壇徽章:
- 0
|
本文提供了一個(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 |
|