最近項目要使用WebService,正在學習中,作個記錄。java
WebService是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可以使用開放的XML(標準通用標記語言下的一個子集)標準來描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操做的應用程序。web
巴拉巴拉。。。。。。shell
本農使用的JDK自帶的WebService編寫。編程
一、開始編寫接口
瀏覽器
package team.soi.service; /** * Created by soi on 15-10-20. */ public interface HelloService { /** * to do sth. * @param to * @return */ Object toDoSth(String to); }
二、編寫接口的實現類app
package team.soi.service.impl; import team.soi.service.HelloService; import javax.jws.WebService; /** * Created by soi on 15-10-20. */ @WebService public class HelloServiceImpl implements HelloService { public Object toDoSth(String to) { return "Hello," + to + "! Welcome to my webservice world!"; } }
三、發佈WebService分佈式
package team.soi; import team.soi.service.impl.HelloServiceImpl; import javax.xml.ws.Endpoint; /** * Hello world! */ public class App { public static void main(String[] args) { Endpoint.publish("http://localhost:8899/ws/demo", new HelloServiceImpl()); } }
奏是介麼仍性,在瀏覽器輸入svn
http://localhost:8899/ws/demo?wsdl
見證奇蹟的時刻到了,Duang......以下:學習
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 (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> <!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. --> <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://impl.service.soi.team/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.soi.team/"name="HelloServiceImplService"> <types> <xsd:schema> <xsd:import namespace="http://impl.service.soi.team/" schemaLocation="http://localhost:8899/ws/demo?xsd=1"/> </xsd:schema> </types> <message name="toDoSth"> <part name="parameters" element="tns:toDoSth"/> </message> <message name="toDoSthResponse"> <part name="parameters" element="tns:toDoSthResponse"/> </message> <portType name="HelloServiceImpl"> <operation name="toDoSth"> <input wsam:Action="http://impl.service.soi.team/HelloServiceImpl/toDoSthRequest" message="tns:toDoSth"/> <output wsam:Action="http://impl.service.soi.team/HelloServiceImpl/toDoSthResponse" message="tns:toDoSthResponse"/> </operation> </portType> <binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="toDoSth"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloServiceImplService"> <port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding"> <soap:address location="http://localhost:8899/ws/demo"/> </port> </service> </definitions>
到這裏,說明你的WebService服務端編寫完成了,接下來,咱們要怎麼去調用呢?測試
說真的,在今天以前,我就知道WebService能夠如上的寫法,可是不會調用,感受本身好Low,因而乎,在羣裏面一吼,不少熱心的人回答了個人問題,可是,沒法知足個人需求。最後問了一個工做上有對接的朋友,得知了接下來該作的事情。
使用JDK的命令:
soi@soi:~/workspace/wsc$ wsimport -extension -keep -p team.soi.ws.client -s ./src -d ./bin http://localhost:8899/ws/demo?wsdl 正在解析 WSDL... 正在生成代碼... 正在編譯代碼... soi@soi:~/workspace/wsc$ cd bin soi@soi:~/workspace/wsc/bin$ jar cvf hello-ws-demo.jar team soi@soi:~/workspace/wsc/bin$ ls hello-ws-demo.jar team
此時,咱們已經拿到了本WebService的客戶端jar包,咱們將客戶端jar包加入到咱們的工程,瓜熟蒂落,開始編寫客戶端代碼:
package team.soi; import junit.framework.TestCase; import team.soi.ws.client.HelloServiceImpl; import team.soi.ws.client.HelloServiceImplService; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * test ws */ public void testWs() { HelloServiceImpl service = new HelloServiceImplService().getHelloServiceImplPort(); String s = (String) service.toDoSth("Soi"); System.out.println(s); } }
運行測試代碼:
Hello,Soi! Welcome to my webservice world!
擼完收工.......