- Chọn file WSDL từ 1 địa chỉ đã công bố (local hoặc online). - Dùng lệnh wsimport của JDK để tạo stub của Webservice tại máy client[r]
Trang 1Đọc dữ liệu từ Webservice (WSDL) theo chuẩn Java Webservice JAX-WS
- Yêu cầu: phải tạo biến môi trường (class path) cho JDK
- Mở Command Promt (Dos trên Windows)
Trang 2Lập trình mạng
- Kiểm tra biến môi trường của java
- Chọn file WSDL từ 1 địa chỉ đã công bố (local hoặc online)
- Dùng lệnh wsimport của JDK để tạo stub của Webservice tại máy client
- File WSDL có nội dung và địa chỉ có dạng như sau:
Trang 3<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://webservice"
xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://webservice"
xmlns:intf="http://webservice" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<! WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT) >
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://webservice"
xmlns="http://www.w3.org/2001/XMLSchema">
<element name="cong">
<complexType>
<sequence>
<element name="a" type="xsd:int"/>
<element name="b" type="xsd:int"/>
</sequence>
</complexType>
</element>
<element name="congResponse">
<complexType>
<sequence>
<element name="congReturn" type="xsd:long"/>
</sequence>
</complexType>
</element>
<element name="nhan">
<complexType>
<sequence>
<element name="a" type="xsd:int"/>
<element name="b" type="xsd:int"/>
</sequence>
</complexType>
</element>
<element name="nhanResponse">
Trang 4Lập trình mạng
</element>
…
</schema>
</wsdl:types>
<wsdl:message name="nhanResponse">
<wsdl:part element="impl:nhanResponse" name="parameters">
</wsdl:part>
</wsdl:message>
…
<wsdl:service name="PhepToanService">
<wsdl:port binding="impl:PhepToanSoapBinding" name="PhepToan">
<wsdlsoap:address location="http://localhost:8080/WS_K6_Server/services/PhepToan"/> </wsdl:port>
</wsdl:service>
</wsdl:definitions>
- Tạo 1 Project client trong Eclipse
- Thư mục src trong Project rỗng
- Di chuyển vào thư mục src trong Project từ cmd
- Gõ lệnh sau để tạo 1 stub của Webservice tại client
Trang 6Lập trình mạng
- TestClient.java
import webservice.PhepToanService;
public class TestClient {
public static void main(String[] args) {
PhepToanService pt = new PhepToanService();
System out println( "Kết quả cộng: " + pt.getPhepToan().cong(2,3)); System out println( "Kết quả nhân: " + pt.getPhepToan().nhan(2, 3)); }
}
Kết quả có dạng:
Kết quả cộng: 5
Kết quả nhân: 6