原文链接: http://www.kylin-ux.com/2017/04/21/network-web-web-service系列之实例之JAX-WS
开发环境
1 2
| ➜ ~ cat /etc/redhat-release Fedora release 25 (Twenty Five)
|
1 2 3 4
| ➜ ~ java -version java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
|
1 2 3 4 5
| ➜ ~ python3 Python 3.5.2 (default, Sep 14 2016, 11:28:32) >>> import suds, zeep >>> suds.__version__, zeep.__version__ ('1.3.3.0', '1.4.1')
|
1 2 3 4
| Eclipse IDE for Java Developers Version: Neon.3 Release (4.6.3) Build id: 20170314-1500
|
suds, zeep 是python web service client
suds ( https://github.com/cackharot/suds-py3 ) version: 1.3.3.0
zeep ( https://github.com/mvantellingen/python-zeep ) version: 1.4.1
JAX-WS webservice endpoint
HelloWorld.java (JAXWSService Endpoint Interface)
1 2 3 4 5 6 7 8 9 10 11
| package com.kylinux.webservice; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface HelloWorld { @WebMethod public String helloWorld(String [] name_temp); }
|
HelloWorldImpl.java (JAXWSService Endpoint implementation class)
1 2 3 4 5 6 7 8 9 10 11
| package com.kylinux.webservice; import javax.jws.WebService; @WebService(endpointInterface = "com.kylinux.webservice.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String helloWorld(String [] name_temp) { return "Hello world from " + name_temp[0] + name_temp[1]; } }
|
HelloWorldWSPublisher.java (Endpoint publisher)
1 2 3 4 5 6 7 8 9
| package com.kylinux.webservice; import javax.xml.ws.Endpoint; public class HelloWorldWSPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:9000/WS/HelloWorld", new HelloWorldImpl()); } }
|
JAXWS Client
生成client stubs, wsimport command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| ➜ ~ project_home=$HOME/workspace/JAXWSClient ➜ ~ cd $project_home/src ➜ src pwd /home/kylin/workspace/JAXWSClient/src ➜ src wsimport -s . http://localhost:9000/WS/HelloWorld\?wsdl parsing WSDL... Generating code... Compiling code... ➜ src tree . └── com └── kylinux └── webservice ├── HelloWorld.class ├── HelloWorldImplService.class ├── HelloWorldImplService.java ├── HelloWorld.java ├── HelloWorldResponse.class ├── HelloWorldResponse.java ├── HelloWorld_Type.class ├── HelloWorld_Type.java ├── ObjectFactory.class ├── ObjectFactory.java ├── package-info.class └── package-info.java 3 directories, 12 files
|
JAXWSClient.java (client class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.kylinux.webservice.client; import java.util.List; import java.util.ArrayList; import com.kylinux.webservice.HelloWorld; import com.kylinux.webservice.HelloWorldImplService; public class JAXWSClient { /** * @author Kylin */ public static void main(String[] args) { HelloWorldImplService helloWorldService = new HelloWorldImplService(); HelloWorld helloWorld = helloWorldService.getHelloWorldImplPort(); List<String> names = new ArrayList<String>(); names.add("Kylin"); names.add("Shu"); System.out.println(helloWorld.helloWorld(names)); } }
|
运行程序
1
| Hello world from KylinShu
|
suds - Python client
1 2 3 4 5 6 7 8 9
| from suds.client import Client ip = '127.0.0.1' port = 9000 client = Client("http://%s:%s/WS/HelloWorld?wsdl" % (ip, port)) #print(client) r = client.service.helloWorld(['Kylin', 'Shu']) print(r)
|
运行结果
1
| Hello world from KylinShu
|
zeep - Python client
1 2 3 4 5 6 7 8 9
| from zeep import Client ip = '127.0.0.1' port = 9000 client = Client("http://%s:%s/WS/HelloWorld?wsdl" % (ip, port)) #print(client.wsdl.dump()) r = client.service.helloWorld(["Kylin", "Shu"]) print(r)
|
运行结果
1
| Hello world from KylinShu
|