WebService入门初使用(二)调用WebService服务
前言
上一节记录完WebService的创建,鉴于篇幅所致,将调用部分单独拿到本节,所调用WebService服务基于上一章节内容。
客户端-调用(Java)
服务发布成功了,如何调用呢?请看说明书-WSDL: 任何一个服务在地址栏输入服务地址加?wsdl 如:http://127.0.0.1:6789/hello ?wsdl 目前不是访问webService,只是获取一个用于描述WebService的说明文件,即:wsdl文件. wsdl- WebService Description Language,是以XML文件形式来描述WebService的”说明书”, 有了说明书,我们才可以知道如何使用或是调用这个服务。
wsimport生成客户端java文件
在cmd中输入类似示例的命令,其中网站部分(http)根据实际情况更改称成自己要调用的WebService的地址:
e:/>wsimport –s . http://localhost:8080/webservice/HelloWorld?wsdl
注意:-s不能分开,-s后面有个小点,用于指定源代码生成的目录。点即当前目录。(注意.前后有空格) 如果使用了-s参数则会在目录下生成两份代码,一份为.class代码。一份为.java代码。 .class代码,可以经过打包以后使用。java代码可以直接Copy到我们的项目中运行。
生成的代码:
[toggle hide=“yes” title=“wsimport.exe简介” color=“red”]
wsimport.exe是jdk自带的,可以根据wsdl文档生成客户端调用java代码,当然如果是用其他语言的类似工具,解析wsdl后将会生成对应语言的代码,这里只是用java为例子,注意这些代码不是通过服务端下载的,而是通过解析wsdl生成对应java文件(就是一个本地IO)。 wsimport.exe位于JAVA_HOME\bin目录下.
常用参数为: -d<目录> - 将生成.class文件。默认参数。 -s<目录> - 将生成.java文件。 -p<生成的新包名> -将生成的类,放于指定的包下:-p com.intsmaze.demo (wsdlurl) - http://server:port/service?wsdl,必须的参数。
1
2
3
4
5
[/toggle] 然后只需要根据wsdl文件提供的信息调用生成类提供的方法。建议从下往上看。 [toggle hide=“yes” title=“简析wsdl文件” color=“red”] 以下内容均为对照java接口调用——webservice就是一个RPC而已,所得结果。所有为注释的单行,若无注明均为对上一行的注释。
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="HelloSevice"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
name="HelloserServiceImplService"
targetNamespace="HelloSevice">
//这是服务端的包结构,一般来说通过注解修改最多,不要暴露出去!(上面那个网站说的,具体不太清楚)
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="HelloSevice"
elementFormDefault="unqualified" targetNamespace="HelloSevice" version="1.0">
<xs:element name="say" type="tns:say"/>
<xs:element name="sayResponse" type="tns:sayResponse"/>
<xs:complexType name="say">
<xs:sequence>
<xs:element minOccurs="0" name="str" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayResponse">//6,子元素说明了它的类型,已经是参数还是返回值
<xs:sequence>
<xs:element minOccurs="0" name="Resultstr" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="say">
<wsdl:part element="tns:say" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayResponse">//5,通过element可以知道参数类型
<wsdl:part element="tns:sayResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IHelloSevice">//3,找到标签它的子元素就是提供的方法
<wsdl:operation name="say">//方法名
<wsdl:input message="tns:say" name="say"></wsdl:input>
//4,输入参数,通过message的属性可以知道参数类型,但是如果生成本地代码,通过调用函数就可以知道参数类型了。
<wsdl:output message="tns:sayResponse" name="sayResponse"></wsdl:output>
//输出参数
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloserServiceImplServiceSoapBinding" type="tns:IHelloSevice">
//2,根据type的属性找到对应的标签
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="say">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="say">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloserServiceImplService">//服务的名称,创建具体服务对象。
<wsdl:port binding="tns:HelloserServiceImplServiceSoapBinding" //1,根据这个名称找到对应的标签
name="HelloserServiceImplPort">
//服务对象调用getHelloserServiceImplPort()获取端口返回服务接口。
<soap:address location="http://localhost:8080/webservice/HelloWorld"/>
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[/toggle]
创建测试类
将第一步导出的java文件导入客户端项目,并创建如下测试类
package cn.wind.WebServiceC.controller;
import cn.wind.webservicee.webservices.HelloserServiceImplService;
import cn.wind.webservicee.webservices.IHelloSevice;
/**
* Created by wind on 2016/11/15.
*/
public class ddd {
public static void main(String[] args) {
/**
* <wsdl:service name="HelloserServiceImplService">
*/
HelloserServiceImplService hss = new HelloserServiceImplService();
/**
*
* <wsdl:port binding="tns:HelloserServiceImplServiceSoapBinding" name="HelloserServiceImplPort">
* <wsdl:binding name="HelloserServiceImplServiceSoapBinding" type="ns1:IHelloSevice">
*/
IHelloSevice soap = hss.getHelloserServiceImplPort();
String str = soap.say("intsmaze",1);//这里我们看视乎在调用我们本地的方法,其实内部把发送数据组装为soap协议,
//然后把数据发送到了服务端,服务端的线程接收到请求处理返回了数据。
System.out.println(str);
}
}
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
除特别注明外,本站所有文章均为 Windcoder网 原创,转载请注明出处来自: webservicerumenchushiyongerdiaoyongwebservicefuwu

No Data