久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Java WebService 簡單實例(附實例代碼)

133次閱讀
沒有評論

共計 1280 個字符,預計需要花費 4 分鐘才能閱讀完成。

下面是一個簡單的 Java WebService 實例,使用了 JAX-WS 標準:

  1. 編寫 WebService 接口:
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
  1. 實現 WebService 接口:
import javax.jws.WebService;
@WebService(endpointInterface = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {return "Hello," + name + "!";}
}
  1. 配置 Web Service 部署描述文件(web.xml):

WebServiceExample

WebServiceExample
com.sun.xml.ws.transport.http.servlet.WSServlet

com.sun.xml.ws.transport.http.servlet.WSServletContextListener
1



WebServiceExample
/hello

  1. 配置 Web Service 綁定描述文件(sun-jaxws.xml):


  1. 使用 JAX-WS 提供的工具生成 Web Service 服務端和客戶端代碼:
wsimport -keep -p com.example.ws http://localhost:8080/WebServiceExample/hello?wsdl
  1. 部署到 Servlet 容器(如 Tomcat)并啟動。
  2. 測試 WebService 服務端:

在瀏覽器中訪問http://localhost:8080/WebServiceExample/hello?wsdl,能夠看到生成的 WSDL 文件。

  1. 測試 WebService 客戶端:
import com.example.ws.HelloWorld;
import com.example.ws.HelloWorldImplService;
public class HelloWorldClient {public static void main(String[] args) {HelloWorldImplService service = new HelloWorldImplService();
HelloWorld port = service.getHelloWorldImplPort();
String result = port.sayHello("Alice");
System.out.println(result);
}
}

這個簡單的 Java WebService 實例演示了一個簡單的問候服務。通過 WebService 接口定義了一個 sayHello 方法,實現類返回一個問候語。客戶端通過調用 WebService 接口的方法與服務端進行交互。

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-21發表,共計1280字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 南漳县| 固原市| 商城县| 大悟县| 荔浦县| 泸水县| 读书| 富平县| 扶沟县| 普陀区| 乌什县| 萍乡市| 中阳县| 白银市| 剑阁县| 桃园县| 巩义市| 通海县| 文安县| 泰和县| 尼玛县| 双城市| 吉安市| 项城市| 长泰县| 铜陵市| 内黄县| 林州市| 拜泉县| 正定县| 静乐县| 文化| 马关县| 金阳县| 连州市| 德兴市| 彰化市| 峡江县| 澄迈县| 当涂县| 湛江市|