標籤:webservice 服務端 用戶端
這幾天一直在看webService相關的知識。
webService是一個跨平台訪問的第三方外掛程式,方便易用,多平台使用。
開發中我們可能自己是服務端,提供webService介面給別人訪問我們的系統;也有可能我們調用別人的webService介面去訪問別人的系統(比如查詢天氣預報)。
下面是服務端和用戶端的開發,實際開發中我們只是其中一方。
服務端開發:
①建立一個服務類,運行main方法發布即可,服務端就開發完成了。
package com.lijianbo.service;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.ws.Endpoint;/** * WebService * 將 Java 類標記為實現 Web Service,或者將 Java 介面標記為定義 Web Service 介面 * 服務類這一個,寫好後運行main方法發布服務即可,服務端就開發完成了。 * */@WebService(serviceName="MyService",targetNamespace="http://www.hello.com")public class HelloService { @WebMethod(operationName="AliassayHello") @WebResult(name="myReturn") public String sayHello(@WebParam(name="name") String name){ return "伺服器,hello: " + name; } public String sayGoodbye(String name){ return "伺服器,goodbye: " + name; } @WebMethod(exclude=true)//當前方法不被發布出去 public String sayHello2(String name){ return "hello" + name; } public static void main(String[] args) { /** * 參數1:服務的發布地址 * 參數2:服務的實現者 * Endpoint 會重新啟動一個線程 */ Endpoint.publish("http://172.18.100.52:456/hello", new HelloService()); System.out.println("****[ Server ready...WebService發布成功。]"); }}
②訪問瀏覽器,網址為:
http://172.18.100.52:456/hello?wsdl
看到如下結果,那麼就說明成功了。
This XML file does not appear to have any style information associated with it. The document tree is shown below.<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy"xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.hello.com"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.hello.com" name="MyService"><types><xsd:schema><xsd:import namespace="http://www.hello.com" schemaLocation="http://172.18.100.52:456/hello?xsd=1"/></xsd:schema></types><message name="AliassayHello"><part name="parameters" element="tns:AliassayHello"/></message><message name="AliassayHelloResponse"><part name="parameters" element="tns:AliassayHelloResponse"/></message><message name="sayGoodbye"><part name="parameters" element="tns:sayGoodbye"/></message><message name="sayGoodbyeResponse"><part name="parameters" element="tns:sayGoodbyeResponse"/></message><portType name="HelloService"><operation name="AliassayHello"><input wsam:Action="http://www.hello.com/HelloService/AliassayHelloRequest" message="tns:AliassayHello"/><output wsam:Action="http://www.hello.com/HelloService/AliassayHelloResponse" message="tns:AliassayHelloResponse"/></operation><operation name="sayGoodbye"><input wsam:Action="http://www.hello.com/HelloService/sayGoodbyeRequest" message="tns:sayGoodbye"/><output wsam:Action="http://www.hello.com/HelloService/sayGoodbyeResponse" message="tns:sayGoodbyeResponse"/></operation></portType><binding name="HelloServicePortBinding" type="tns:HelloService"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="AliassayHello"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation><operation name="sayGoodbye"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="MyService"><port name="HelloServicePort" binding="tns:HelloServicePortBinding"><soap:address location="http://172.18.100.52:456/hello"/></port></service></definitions>
用戶端開發:
①cmd 進入 命令列。
輸入:
wsimport -d c: -keep -verbose http://172.18.100.52:456/hello?wsdl
它有幾個很重要的參數,
-d 表示輸出的目錄,目錄必須事先存在,否則匯出失敗。
-keep表示匯出webservice的class檔案時是否也匯出原始碼Java檔案。
-verbose表示詳細資料。
看我們的匯出命令。我們直接導在C盤中。
顯示:
C:\>wsimport -d c: -keep -verbose http://172.18.100.52:456/hello?wsdlparsing WSDL...Generating code...com\hello\AliassayHello.javacom\hello\AliassayHelloResponse.javacom\hello\HelloService.javacom\hello\MyService.javacom\hello\ObjectFactory.javacom\hello\SayGoodbye.javacom\hello\SayGoodbyeResponse.javacom\hello\package-info.java
這樣用戶端的代碼就產生了。我們是產生在C盤下的,拷貝產生的程式碼到項目。
②運行測試類別,調用webService介面
package com.lijianbo.client;import java.rmi.RemoteException;import javax.xml.rpc.ServiceException;import com.hello.HelloService;import com.hello.MyService;public class TestHello { /** * 通過wsimport 解析wsdl產生用戶端代碼調用WebService服務 * * 運行測試類別就能成功調用伺服器的介面,得到返回資料。 * */ public static void main(String[] args) throws ServiceException, RemoteException { /** * <service name="MyService"> * 獲得服務名稱 */ MyService mywebService = new MyService(); /** * <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> */ HelloService hs = mywebService.getHelloServicePort(); System.out.println("hs:"+hs); /** * 調用方法 */ System.out.println(hs.sayGoodbye("sjk=============")); System.out.println(hs.aliassayHello("sjk================")); }}
運行結果為:
hs:JAX-WS RI 2.2.4-b01: Stub for http://172.18.100.52:456/hello伺服器,goodbye: sjk=============伺服器,hello: sjk================
上面可以看出,這裡成功訪問到了伺服器的介面,返回了資料。說明調用webService成功了。
webService基本的知識點就是這樣了,當時實際中服務端代碼是發布在伺服器的,而不是本機。
用戶端代碼也可以採用調用產生好的jar包(服務端產生)來使用。
實際開發中,用戶端往往還要根據SOAP文檔來拼接參數,通過SOAP協議進行參數傳遞,然後採用用戶端代理類來調用介面,得到返回的資料。
本文出自 “JianBo” 部落格,轉載請與作者聯絡!
webService服務端和用戶端開發 簡單一實例