本文提供了一個使用Java如何開發 基于SOAP的Web Services,其用戶端可以是Perl、Ruby、Python或Java等。
Java SE 6封裝了JAX-WS(Java API for XML-Web Services),而JAX-WS同時支援基于SOAP的Web服務和REST風格的Web服務。JAX-WS通常可簡寫為JWS,目前,JWS的版本為2.x。
基于SOAP的Web服務可用單個Java類的實作,但是最好是用“接口+實作”的方式來實作最佳。
Web服務的接口稱為SEI,即Service Endpoint Interface;
而Web服務的實作稱為SIB,即Service Implementation Bean。
SIB可以是一個POJO,也可以是無狀态的會話EJB。本文的SIB是普通Java類,通過JDK 6的類庫即可實作Web服務的釋出。
代碼1:服務接口類SEI
view plaincopy to clipboardprint?
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:服務實作類SIB
view plaincopy to clipboardprint?
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 {
public long getTimeAsElapsed() {
return new Date().getTime();
}
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 {
public long getTimeAsElapsed() {
return new Date().getTime();
}
public String getTimeAsString() {
Date date = new Date();
DateFormat df = DateFormat.getDateInstance();
return df.format(date);
}
}
代碼3:服務釋出類Publisher
view plaincopy to clipboardprint?
package myweb.service;
import javax.xml.ws.Endpoint;
public class TimeServerPublisher {
public static void main(String[] args){
// 第一個參數是釋出的URL
// 第二個參數是SIB實作
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){
// 第一個參數是釋出的URL
// 第二個參數是SIB實作
Endpoint.publish("http://127.0.0.1:10100/myweb ", new TimeServerImpl());
}
}
編譯以上代碼 :
javac myweb/service/*.java
運作服務:
java myweb/service/TimeServerPublisher
在浏覽器位址欄輸入:
http://localhost:10100/myweb?wsdl
顯示如下圖所示:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauIUWWtWMzUzMzkDMzIjMxkDMwIzXx8CXyEzLclDMwIzLcNHduVWboNWY0RXYvwVbvNmLh5WaoNGcoBnL3d3dvw1LcpDc0RHaiojIsJye.jpg)
也可編寫用戶端代碼測試服務。
Java用戶端:
view plaincopy to clipboardprint?
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 ");
// 第一個參數是服務的URI
// 第二個參數是在WSDL釋出的服務名
QName qname = new QName("http://service.myweb/","TimeServerImplService ");
// 建立服務
Service service = Service.create(url, qname);
// 提取端點接口,服務“端口”。
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 ");
// 第一個參數是服務的URI
// 第二個參數是在WSDL釋出的服務名
QName qname = new QName("http://service.myweb/","TimeServerImplService ");
// 建立服務
Service service = Service.create(url, qname);
// 提取端點接口,服務“端口”。
TimeServer eif = service.getPort(TimeServer.class);
System.out.println(eif.getTimeAsString());
System.out.println(eif.getTimeAsElapsed());
}
}
運作用戶端,顯示結果如下:
2009-12-21
1261402511859
也可用Ruby編寫用戶端,如下:
view plaincopy to clipboardprint?
#!/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}"
運作結果相同!
本文來自CSDN部落格,轉載請标明出處:http://blog.csdn.net/chszs/archive/2009/12/21