JAVA 6 的出現,拯救了像我這種怕麻煩的人 ...
關鍵在於兩個 annotation: @WebService 和 @WebMethod
我參考了 Vivek Pandey's Blog 和 学习Java6(一) WebServices(3)在tomcat中发布,簡單地整理開發 Webservice 的 Server 端和 Client 端程式的過程,以及如何部屬到 Tomcat:
參考 Vivek 的範例,寫一個加法的 Webservice
- Server Side
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;
@WebService(targetNamespace = "http://localhost/sample")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Calculator {
@WebMethod
public int add(int a, int b) {
return a+b;
}
}
- Deploy
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet>
<servlet-name>WebServiceStarter</servlet-name>
<servlet-class>WebServiceStarter</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
在 WEB-INF 裡面在建立 classes 的目錄,把編譯好的 Calculator.class 和 WebServiceStarter.class 複製到裡面,重新啟動 Tomcat。檢視 Tomcat 的 log :
catalina.out
Jun 29, 2007 5:16:16 PM org.apache.catalina.core.AprLifecycleListener init
INFO: 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/lib
Jun 29, 2007 5:16:16 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Jun 29, 2007 5:16:16 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 875 ms
Jun 29, 2007 5:16:16 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Jun 29, 2007 5:16:16 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.13
Starting Calculator Service ......
Calculator Service is OK!
Jun 29, 2007 5:16:18 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jun 29, 2007 5:16:18 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 29, 2007 5:16:18 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/43 config=null
Jun 29, 2007 5:16:18 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2077 ms
這樣 Server 端就 OK 了!
- Java Client
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
測試網址:http://ir.tmu.edu.tw/calc/
有沒有很簡單呢,呵呵!