以前的經驗,開發 Webservice 的程式是一件相當麻煩的事情,環境設定、編輯 WSDL、程式佈署等等,即使用 IDE 的工具,都不是三言兩語就可以搞定。
JAVA 6 的出現,拯救了像我這種怕麻煩的人 ...
關鍵在於兩個 annotation: @WebService 和 @WebMethod
參考了 Vivek Pandey's Blog 和 学习Java6(一) WebServices(3)在tomcat中发布,簡單地整理開發 Webservice 的 Server 端和 Client 端程式的過程,以及如何部屬到 Tomcat:
參考 Vivek 的範例,寫一個加法的 Webservice
Server SideWebServiceStarter.java
public class
WebServiceStarter extends HttpServlet{
private static final long serialVersionUID = 5870534239093709659L;
public WebServiceStarter() {
super();
}
public void destroy() {
super.destroy();
}
public void init() throws ServletException{
System.out.println("\nStarting Calculator Service ......\n");
try{
Endpoint.publish("http://localhost:8088/calculator", new Calculator());
}catch (Exception e){
System.out.println("Caught Exception: " + e.toString());
}
System.out.println("\nCalculator Service is OK!\n");
}
}
在 Endpoint.publish 的部份,原本以為 Port 的設定應該與 Tomcat 的預設值 8080 一樣,但是在啟動 Tomcat 的時候發現會跟 Tomcat 搶 8080 Port,所以我改成 8088。
加法的 Service:Calculator.java
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
public class
Calculator{
@WebService(targetNamespace = "http://localhost/sample")@SOAPBinding(style = SOAPBinding.Style.RPC)public class Calculator {
@WebMethodpublic int add(int a, int b) {
return a+b;
}
}
Deploy
程式編譯好後,在 Tomcat 的 webapps 目錄下建立一個 Service 的目錄,如: sample,其內在建立 WEB-INF 目錄,在此目錄裡建立 web.xml:
web.xml
web.xml
WebServiceStarter
WebServiceStarter
1
在 WEB-INF 裡面在建立 classes 的目錄,把編譯好的 Calculator.class 和 WebServiceStarter.class 複製到裡面,重新啟動 Tomcat。檢視 Tomcat 的 log :
catalina.out
Jun 29, 2007 5:16:16 PM org.apache.catalina.core.AprLifecycleListener initINFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.00/jre/lib/i386/client:/usr/lib/jvm/java-6-sun-1.6.0.00/jre/lib/i386:/usr/lib/jvm/java-6-sun-1.6.0.00/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/libJun 29, 2007 5:16:16 PM org.apache.coyote.http11.Http11Protocol initINFO: Initializing Coyote HTTP/1.1 on http-8080Jun 29, 2007 5:16:16 PM org.apache.catalina.startup.Catalina loadINFO: Initialization processed in 875 msJun 29, 2007 5:16:16 PM org.apache.catalina.core.StandardService startINFO: Starting service CatalinaJun 29, 2007 5:16:16 PM org.apache.catalina.core.StandardEngine startINFO: Starting Servlet Engine: Apache Tomcat/6.0.13Starting Calculator Service ......Calculator Service is OK!Jun 29, 2007 5:16:18 PM org.apache.coyote.http11.Http11Protocol startINFO: Starting Coyote HTTP/1.1 on http-8080Jun 29, 2007 5:16:18 PM org.apache.jk.common.ChannelSocket initINFO: JK: ajp13 listening on /0.0.0.0:8009Jun 29, 2007 5:16:18 PM org.apache.jk.server.JkMain startINFO: Jk running ID=0 time=0/43 config=nullJun 29, 2007 5:16:18 PM org.apache.catalina.startup.Catalina startINFO: Server startup in 2077 ms這樣 Server 端就 OK 了!
Java Client先產生需要的 class:
wsimport -keep http://localhost:8088/calculator?wsdl
CalculatorApp.java
class
CalculatorApp {
public static void main(String args[]){
/*** Instantiate the generated Service*/ CalculatorService service = new CalculatorService();
/*** Get the port using port getter method generated in CaculatorService*/Calculator calculatorProxy = service.getCalculatorPort();
/*** Invoke the remote method*/int result = calculatorProxy.add(10, 20);
System.out.println("Sum of 10+20 = "+result);
}
}
編譯,執行,結果應該是:Sum of 10+20 = 30
Flex Client
用 Flex 做一個比較好用的 GUI:
測試網址:http://ir.tmu.edu.tw/calc/
有沒有很簡單呢,呵呵!