最近项目要使用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!
撸完收工.......