1. 程式人生 > >使用wsimport和JAX-WS調用Web Service接口

使用wsimport和JAX-WS調用Web Service接口

rate 代理 sep with 高版本 ini const related img

本文簡單舉例說明如何使用wsimport工具和JAX-WS API調用Web Service接口。
此方法的優點:使用JDK自帶的工具和API接口,無需依賴第三方庫。


JDK版本:1.8.0_141
開發工具:Eclipse

技術分享

服務端源代碼:下載
客戶端源代碼:下載


1. 使用JDK自帶的wsimport工具根據WSDL生成web service client stub
1.1. 確保已安裝JDK1.6版本或更高版本
1.2. 確保WebService服務已經啟動
1.3. 在命令行運行如下命令:

技術分享
參數說明:
-d 指定生成輸出文件的保存路徑(.class文件,根據需要決定是否生成class文件)
-s 指定生成的java源文件的保存路徑(.java文件,根據需要決定是否生成java源文件)
-p 指定生成的java類的包(package)名稱
http://localhost:8888/HelloService表示WebService URL地址,URL地址後面必須添加“?WSDL”參數。WSDL參數也可以是小寫(wsdl)。
要查看服務具體提供了哪些操作,請在URL後面添加“?Tester”參數。(有時候傳遞Tester並不一定能返回結果。這和服務的發布方式有關)
1.4. 查看生成的文件
如圖所示,輸出的文件保存在按指定的包名稱構成的路徑中(jaxws\client\stub)。
技術分享


2. 把生成的client stub類導入到eclipse工程中。
技術分享

3. 編寫客戶端代碼
創建jaxws.client. HelloAppClient類。
客戶端代碼入下所示:

package jaxws.client;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import jaxws.client.stub.HelloService;

public class HelloAppClient { public static void main(String[] args) { sayHello(); } public static void sayHello() { URL url = null; try { url = new URL("http://localhost:8888/HelloService"); } catch (MalformedURLException e) { e.printStackTrace(); } Service s
= Service.create(url, new QName("http://service.jaxws/", "HelloServiceService")); HelloService hs = s.getPort(new QName("http://service.jaxws/", "HelloServicePort"), HelloService.class); String helloMessage = hs.sayHello("Tom"); System.out.println(helloMessage); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }

註意:WebService中所說的Port和網絡端口(port)不是一個概念。
以下WSDL文檔中的一些簡單的概念解釋(如果想深入了解請查看WSDL標準):

A WSDL document defines services as collections of network endpoints, or ports. In WSDL, the abstract definition of endpoints and messages is separated from their concrete network deployment or data format bindings. This allows the reuse of abstract definitions: messages, which are abstract descriptions of the data being exchanged, and port types which are abstract collections of operations. The concrete protocol and data format specifications for a particular port type constitutes a reusable binding. A port is defined by associating a network address with a reusable binding, and a collection of ports define a service. Hence, a WSDL document uses the following elements in the definition of network services:

  • Types– a container for data type definitions using some type system (such as XSD).
  • Message– an abstract, typed definition of the data being communicated.
  • Operation– an abstract description of an action supported by the service.
  • Port Type–an abstract set of operations supported by one or more endpoints.
  • Binding– a concrete protocol and data format specification for a particular port type.
  • Port– a single endpoint defined as a combination of a binding and a network address.
  • Service– a collection of related endpoints.

代碼說明:
(1)url表示WebService服務地址。此地址後面可以添加也可以不添加“?WSDL”參數。
(2)Service提供WebService服務的客戶端視圖(client view)。它作為endpoint的客戶端代理,用於分發(dispatch)對遠程Service操作(Operation)的調用(通俗的說就是把某個Service上的方法調用發送給遠程服務器上提供服務的那個Service)。
(3)HelloService表示WebService服務client stub(類似於WebService服務在客戶端的一個鏡像,供客戶端代碼調用其接口)。
(4)通過hs.sayHello方法調用服務接口。

使用wsimport和JAX-WS調用Web Service接口