使用 javax.jws.WebService 註解標記服務類,Endpoint.publish 發布服務。
package cn.happy.webservice;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class Service {
public void say(String name) {
System.out.println("Hello " + name);
}
public static void main(String[] args) {
Endpoint.publish("https://localhost:50000/hello", new Service());
System.out.println("server is listening ....");
}
}
在瀏覽器訪問 https://localhost:50000/hello?wsdl 獲取 WSDL 文檔,使用工具(如 IDE 內建或 wsimport)生成客戶端存根類。


使用生成的 ServiceService 與 Service 介面調用遠端方法。
package test;
import cn.happy.service.Service;
import cn.happy.service.ServiceService;
public class MyTest {
public static void main(String[] args) {
ServiceService service = new ServiceService();
Service port = service.getServicePort();
port.say("好好好");
}
}
https://192.168.15.54:50000/hello 可查看服務資訊頁面。

總結: JAX-WS 使 SOAP 服務發布與調用極其簡單,Endpoint.publish 一行代碼即可啟動輕量級 HTTP 伺服器,適合快速原型開發與測試。