手把手教你玩转WebService(一)
这个系列文章将教你如何使用Apache CXF开发基于SOAP协议的WebService,并将用到多好玩的开发工具。
本文为开篇,我决定先通过一个Demo,向你展示一个WebService的实例。请确保你的机器上安装了这些工具:
* git
* java
* ant
* eclipse for javaee
* tcpmon
以下是我的工作环境:
git
git将用于从github上面签出demo项目。下面是我的环境:
java
我的java环境如下:
ant
ant是java的编译工具,网址位于:
http://ant.apache.org/
http://www.eclipse.org/downloads/moreinfo/jee.php
http://ws.apache.org/commons/tcpmon/download.cgi
tcpmon-1.0-bin/build$ lstcpmon-1.0.jar tcpmon.bat tcpmon.sh
https://github.com/liweinan/try-cxf-soap
git clone git://github.com/liweinan/try-cxf-soap.git
ant server
ant client
http://127.0.0.1:8080/ws/sayHello?wsdl
File -> New -> Project...
HelloService_HelloServiceSOAP_Client.java
public static void main(String args[]) throws java.lang.Exception {HelloService_Service ss = new HelloService_Service((new File(args[0])).toURI().toURL(), SERVICE_NAME);HelloService port = ss.getHelloServiceSOAP();BindingProvider bp = (BindingProvider) port;Map<String, Object> context = bp.getRequestContext();context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,"http://localhost:1234/ws/sayHello");{System.out.println("Invoking sayHello...");net.bluedash.ws.HelloRequest _sayHello_request = new HelloRequest();_sayHello_request.setWho("world");java.lang.String _sayHello__return = port.sayHello(_sayHello_request);System.out.println("sayHello.result=" + _sayHello__return);}System.exit(0);}
BindingProvider bp = (BindingProvider) port;Map<String, Object> context = bp.getRequestContext();context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,"http://localhost:1234/ws/sayHello");
ant client
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body> <ns2:helloRequest xmlns:ns2="http://bluedash.net/ws"> <who>world</who> </ns2:helloRequest></soap:Body></soap:Envelope>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body> <ns2:helloResponse xmlns:ns2="http://bluedash.net/ws">Hello, world</ns2:helloResponse></soap:Body></soap:Envelope>