WebService 从“心”定义

【特别说明:本文档仅属个人心得,大家各取所需】

什么是WebService

1
2
3
4
5
6
7
8
9
10
DL:听过webservice吗?公司业务需要你来搞一下吧
W:emmm。。。。
W:打开Chrome,找到CSDN,搜索“怎么写webservice”,回车;。。。 。。。
W:哇,真是博大精深,“云雾缭绕”。。。。。。
W:研究了一篇又一篇。。。。。。
DL:做好了吗?
W:快了,快了;
DL:懂了吗?什么是webservice?要如何开发一个webservice服务呢?
W:它是能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据的一种技术。
DL:理解了吗?

嗯?理解了吗?
webservice官网
打开这个官网,看不到任何醒目的解释。所以大可不必去找到底什么是webservice,请记住下面三个关键字:
跨编程语言 + 跨操作系 + 远程调用
记住了吗?

  • 是的,我记住了

没记住就再看一遍,记住了请打钩!!!

记住的人才可以看下面的内容,没记住请继续记
三个关键字简明扼要的表达了该技术的NB,那到底真的有那么厉害吗?

跨编程语言:通俗点说就是PHP可以调用Java写好的webservice,Java可以调用C#写好的webservice…
跨操作系统:这个比较好理解,可以用Linux服务器调用Windows发布的webservice…
远程调用:举个栗子,你认识了很多不同地方的小姐姐,你可以通过A地给B地的张某某打招呼,也可以从A地给C地的李某某送祝福…,谨慎使用哈!(因为远程调用需要注意安全)

怎么写webservice?

写之前一定要知道的几个术语:
客户端:调用这个WebService的应用程序
服务端:提供这个WebService的应用程序
当两端不管用什么编程语言或者在何种操作系统上,只要均保持统一的一套标准(规范),那么“跨出一小步”就易如反掌了。

知道了客户端和服务端,以及大致明白了它们如何达到该技术特性的,怕大家不太理解,下面搞个图分析一下:

image-20211006185419021
看图必懂

这套标准到底有多神奇呢?
1、XML+XSD
XML格式封装数据(XML中说明调用远程服务对象的哪个方法,传递的参数是什么,以及服务对象的 返回结果是什么)优点在于它与平台、厂商无关。
XML Schema(XSD)是一套数据类型标准。确保数据能有效传输和有效读取。
2、SOAP
SOAP协议 = HTTP协议 + XML数据格式
SOAP协议定义了SOAP消息的格式,SOAP协议是基于HTTP协议的,SOAP也是基于XML和XSD的,XML是SOAP的数据编码方式。
举个栗子更好理解:HTTP就是普通公路,XML就是中间的绿色隔离带和两边的防护栏,SOAP就是普通公路经过加隔离带和防护栏改造过的高速公路。
3、WSDL
“物品清单(使用说明书)”:告诉客户端(调用的人)我的地址,我有哪些方法,要怎么调用,参数是什么等。
WSDL(Web Services Description Language)是基于XML的语言,用于描述Web Service及其函数、参数和返回值的文件,通过一个url地址访问。

注意:WebService三大规范:JAXM&SAAJ、JAX-WS(JAX-RPC)、JAX-RS。

到这里,我觉得有必要补一下全网很多帖子不足的地方,那就是WSDL的解析,如下:
WSDL 文档是利用这些主要的元素来描述某个 web service 的:

1
2
3
4
<portType>	web service 执行的操作
<message> web service 使用的消息
<types> web service 使用的数据类型
<binding> web service 使用的通信协议

了解了基本元素的含义后,我们上一个还热乎的wsdl文件:
天气预报wsdl

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
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://WebXml.com.cn/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://WebXml.com.cn/">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a> <strong>2400多个城市天气预报Web服务</strong>,包含2300个以上中国城市和100个以上国外城市天气预报数据。数据每2.5小时左右自动更新一次,准确可靠。<br />使用本站 WEB 服务请注明或链接本站:<a href="http://www.webxml.com.cn/" target="_blank">http://www.webxml.com.cn/</a> 感谢大家的支持!<br /><br /><img alt="PDF" title="PDF file" src="http://www.webxml.com.cn/images/icon/pdf.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/files/WeatherWsHelp.pdf" target="_blank">接口帮助文档</a> &nbsp;&nbsp;&nbsp; <img alt="ZIP" title="ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/files/about_city.zip">部分城市介绍和气候背景</a> &nbsp;&nbsp;&nbsp; <img alt="ZIP" title="ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/files/city_photo.zip">部分城市图片</a> &nbsp;&nbsp;&nbsp; <img alt="HTML" title="HTML file" src="http://www.webxml.com.cn/images/icon/html.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank">天气现象和图例</a><br />&nbsp;</wsdl:documentation>
<wsdl:types>
...
</wsdl:types>
<wsdl:message name="getRegionDatasetSoapIn">
...
</wsdl:message>
...(太多了,省略)
<wsdl:portType name="WeatherWSSoap">
<wsdl:operation name="getRegionDataset">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得中国省份、直辖市、地区;国家名称(国外)和与之对应的ID</h3><p>输入参数:无,返回数据:DataSet。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionDatasetSoapIn"/>
<wsdl:output message="tns:getRegionDatasetSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得中国省份、直辖市、地区和与之对应的ID</h3><p>输入参数:无,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionProvinceSoapIn"/>
<wsdl:output message="tns:getRegionProvinceSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得国外国家名称和与之对应的ID</h3><p>输入参数:无,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionCountrySoapIn"/>
<wsdl:output message="tns:getRegionCountrySoapOut"/>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得支持的城市/地区名称和与之对应的ID</h3><p>输入参数:theRegionCode = 省市、国家ID或名称,返回数据:DataSet。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getSupportCityDatasetSoapIn"/>
<wsdl:output message="tns:getSupportCityDatasetSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得支持的城市/地区名称和与之对应的ID</h3><p>输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getSupportCityStringSoapIn"/>
<wsdl:output message="tns:getSupportCityStringSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getWeather">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得天气预报数据</h3><p>输入参数:城市/地区ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getWeatherSoapIn"/>
<wsdl:output message="tns:getWeatherSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="WeatherWSHttpGet">
<wsdl:operation name="getRegionDataset">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得中国省份、直辖市、地区;国家名称(国外)和与之对应的ID</h3><p>输入参数:无,返回数据:DataSet。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionDatasetHttpGetIn"/>
<wsdl:output message="tns:getRegionDatasetHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得中国省份、直辖市、地区和与之对应的ID</h3><p>输入参数:无,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionProvinceHttpGetIn"/>
<wsdl:output message="tns:getRegionProvinceHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得国外国家名称和与之对应的ID</h3><p>输入参数:无,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionCountryHttpGetIn"/>
<wsdl:output message="tns:getRegionCountryHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得支持的城市/地区名称和与之对应的ID</h3><p>输入参数:theRegionCode = 省市、国家ID或名称,返回数据:DataSet。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getSupportCityDatasetHttpGetIn"/>
<wsdl:output message="tns:getSupportCityDatasetHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得支持的城市/地区名称和与之对应的ID</h3><p>输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getSupportCityStringHttpGetIn"/>
<wsdl:output message="tns:getSupportCityStringHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="getWeather">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得天气预报数据</h3><p>输入参数:城市/地区ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getWeatherHttpGetIn"/>
<wsdl:output message="tns:getWeatherHttpGetOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="WeatherWSHttpPost">
<wsdl:operation name="getRegionDataset">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得中国省份、直辖市、地区;国家名称(国外)和与之对应的ID</h3><p>输入参数:无,返回数据:DataSet。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionDatasetHttpPostIn"/>
<wsdl:output message="tns:getRegionDatasetHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得中国省份、直辖市、地区和与之对应的ID</h3><p>输入参数:无,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionProvinceHttpPostIn"/>
<wsdl:output message="tns:getRegionProvinceHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得国外国家名称和与之对应的ID</h3><p>输入参数:无,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getRegionCountryHttpPostIn"/>
<wsdl:output message="tns:getRegionCountryHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得支持的城市/地区名称和与之对应的ID</h3><p>输入参数:theRegionCode = 省市、国家ID或名称,返回数据:DataSet。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getSupportCityDatasetHttpPostIn"/>
<wsdl:output message="tns:getSupportCityDatasetHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得支持的城市/地区名称和与之对应的ID</h3><p>输入参数:theRegionCode = 省市、国家ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getSupportCityStringHttpPostIn"/>
<wsdl:output message="tns:getSupportCityStringHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="getWeather">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><br /><h3>获得天气预报数据</h3><p>输入参数:城市/地区ID或名称,返回数据:一维字符串数组。</p><br /></wsdl:documentation>
<wsdl:input message="tns:getWeatherHttpPostIn"/>
<wsdl:output message="tns:getWeatherHttpPostOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WeatherWSSoap" type="tns:WeatherWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getRegionDataset">
<soap:operation soapAction="http://WebXml.com.cn/getRegionDataset" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<soap:operation soapAction="http://WebXml.com.cn/getRegionProvince" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<soap:operation soapAction="http://WebXml.com.cn/getRegionCountry" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<soap:operation soapAction="http://WebXml.com.cn/getSupportCityDataset" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<soap:operation soapAction="http://WebXml.com.cn/getSupportCityString" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getWeather">
<soap:operation soapAction="http://WebXml.com.cn/getWeather" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="WeatherWSSoap12" type="tns:WeatherWSSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getRegionDataset">
<soap12:operation soapAction="http://WebXml.com.cn/getRegionDataset" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<soap12:operation soapAction="http://WebXml.com.cn/getRegionProvince" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<soap12:operation soapAction="http://WebXml.com.cn/getRegionCountry" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<soap12:operation soapAction="http://WebXml.com.cn/getSupportCityDataset" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<soap12:operation soapAction="http://WebXml.com.cn/getSupportCityString" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getWeather">
<soap12:operation soapAction="http://WebXml.com.cn/getWeather" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="WeatherWSHttpGet" type="tns:WeatherWSHttpGet">
<http:binding verb="GET"/>
<wsdl:operation name="getRegionDataset">
<http:operation location="/getRegionDataset"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<http:operation location="/getRegionProvince"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<http:operation location="/getRegionCountry"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<http:operation location="/getSupportCityDataset"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<http:operation location="/getSupportCityString"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getWeather">
<http:operation location="/getWeather"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="WeatherWSHttpPost" type="tns:WeatherWSHttpPost">
<http:binding verb="POST"/>
<wsdl:operation name="getRegionDataset">
<http:operation location="/getRegionDataset"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionProvince">
<http:operation location="/getRegionProvince"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRegionCountry">
<http:operation location="/getRegionCountry"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityDataset">
<http:operation location="/getSupportCityDataset"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getSupportCityString">
<http:operation location="/getSupportCityString"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getWeather">
<http:operation location="/getWeather"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WeatherWS">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"><a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a> <strong>2400多个城市天气预报Web服务</strong>,包含2300个以上中国城市和100个以上国外城市天气预报数据。数据每2.5小时左右自动更新一次,准确可靠。<br />使用本站 WEB 服务请注明或链接本站:<a href="http://www.webxml.com.cn/" target="_blank">http://www.webxml.com.cn/</a> 感谢大家的支持!<br /><br /><img alt="PDF" title="PDF file" src="http://www.webxml.com.cn/images/icon/pdf.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/files/WeatherWsHelp.pdf" target="_blank">接口帮助文档</a> &nbsp;&nbsp;&nbsp; <img alt="ZIP" title="ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/files/about_city.zip">部分城市介绍和气候背景</a> &nbsp;&nbsp;&nbsp; <img alt="ZIP" title="ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/files/city_photo.zip">部分城市图片</a> &nbsp;&nbsp;&nbsp; <img alt="HTML" title="HTML file" src="http://www.webxml.com.cn/images/icon/html.gif" style="vertical-align: middle;" /> <a href="http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank">天气现象和图例</a><br />&nbsp;</wsdl:documentation>
<wsdl:port name="WeatherWSSoap" binding="tns:WeatherWSSoap">
<soap:address location="http://www.webxml.com.cn/WebServices/WeatherWS.asmx"/>
</wsdl:port>
<wsdl:port name="WeatherWSSoap12" binding="tns:WeatherWSSoap12">
<soap12:address location="http://www.webxml.com.cn/WebServices/WeatherWS.asmx"/>
</wsdl:port>
<wsdl:port name="WeatherWSHttpGet" binding="tns:WeatherWSHttpGet">
<http:address location="http://www.webxml.com.cn/WebServices/WeatherWS.asmx"/>
</wsdl:port>
<wsdl:port name="WeatherWSHttpPost" binding="tns:WeatherWSHttpPost">
<http:address location="http://www.webxml.com.cn/WebServices/WeatherWS.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

上面是一个线上可以调用的wsdl文件,其中部分省略,看完你可能要说:我*,这么长,怎么看得懂。别怕,我也看不懂,篇幅太长,无从下手,所以我花了很大时间特地简化了一下,把非核心的内容暂时清楚,如下:

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
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!--文档描述: 说明服务的各种信息,根节点,如果你只是用的话,可以不深究每一项的含义-->
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://WebXml.com.cn/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://WebXml.com.cn/">
<!--各种文档的介绍,内容各不相同,可以不做研究,删掉-->
<wsdl:documentation
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
...
</wsdl:documentation>
<!--各种数据类型的描述,通过s:schema指定默认元素、命名空间,各种数据类型由definitions中引入大类:指定具体数据类型(太长,仅留部分)-->
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://WebXml.com.cn/">
<s:element name="getRegionDataset">
<s:complexType/>
</s:element>
<s:element name="getRegionProvince">
<s:complexType/>
</s:element>
<s:element name="getRegionProvinceResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getRegionProvinceResult" type="tns:ArrayOfString"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfString">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="s:string"/>
</s:sequence>
</s:complexType>
<s:element name="ArrayOfString" nillable="true" type="tns:ArrayOfString"/>
</s:schema>
</wsdl:types>
<!--函数参数(输入与输出分开)的描述-->
<wsdl:message name="getRegionDatasetSoapIn">
<wsdl:part name="parameters" element="tns:getRegionDataset"/>
</wsdl:message>
<wsdl:message name="getSupportCityStringHttpPostIn">
<wsdl:part name="theRegionCode" type="s:string"/>
</wsdl:message>
<wsdl:message name="getSupportCityStringHttpPostOut">
<wsdl:part name="Body" element="tns:ArrayOfString"/>
</wsdl:message>
<wsdl:message name="getWeatherHttpPostIn">
<wsdl:part name="theCityCode" type="s:string"/>
<wsdl:part name="theUserID" type="s:string"/>
</wsdl:message>
<wsdl:message name="getWeatherHttpPostOut">
<wsdl:part name="Body" element="tns:ArrayOfString"/>
</wsdl:message>
<!--引用消息部分中消息定义来描述函数签名(操作名、输入参数、输出参数)-->
<wsdl:portType name="WeatherWSHttpGet">
<wsdl:operation name="getRegionDataset">
<wsdl:documentation
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br />
<h3>获得中国省份、直辖市、地区;国家名称(国外)和与之对应的ID</h3>
<p>输入参数:无,返回数据:DataSet。</p>
<br />
</wsdl:documentation>
<wsdl:input message="tns:getRegionDatasetHttpGetIn"/>
<wsdl:output message="tns:getRegionDatasetHttpGetOut"/>
</wsdl:operation>
</wsdl:portType>
<!--PortTypes部分的每一操作在此绑定实现-->
<wsdl:binding name="WeatherWSHttpPost" type="tns:WeatherWSHttpPost">
<http:binding verb="POST"/>
<wsdl:operation name="getRegionDataset">
<http:operation location="/getRegionDataset"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<!--说明每一绑定的端口地址-->
<wsdl:service name="WeatherWS">
<wsdl:documentation
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a>
<strong>2400多个城市天气预报Web服务</strong>,包含2300个以上中国城市和100个以上国外城市天气预报数据。数据每2.5小时左右自动更新一次,准确可靠。
<br />使用本站 WEB 服务请注明或链接本站:
<a href="http://www.webxml.com.cn/" target="_blank">http://www.webxml.com.cn/</a> 感谢大家的支持!
<br />
<br />
<img alt="PDF" title="PDF file" src="http://www.webxml.com.cn/images/icon/pdf.gif" style="vertical-align: middle;" />
<a href="http://www.webxml.com.cn/files/WeatherWsHelp.pdf" target="_blank">接口帮助文档</a> &nbsp;&nbsp;&nbsp;
<img alt="ZIP" title="ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style="vertical-align: middle;" />
<a href="http://www.webxml.com.cn/files/about_city.zip">部分城市介绍和气候背景</a> &nbsp;&nbsp;&nbsp;
<img alt="ZIP" title="ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style="vertical-align: middle;" />
<a href="http://www.webxml.com.cn/files/city_photo.zip">部分城市图片</a> &nbsp;&nbsp;&nbsp;
<img alt="HTML" title="HTML file" src="http://www.webxml.com.cn/images/icon/html.gif" style="vertical-align: middle;" />
<a href="http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank">天气现象和图例</a>
<br />&nbsp;
</wsdl:documentation>
<wsdl:port name="WeatherWSHttpPost" binding="tns:WeatherWSHttpPost">
<http:address location="http://www.webxml.com.cn/WebServices/WeatherWS.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

怎么样?这样删减并格式化后看起来是不是就很清楚了?如果你还没有明白,那记住下面的诀窍:
1、从下往上看,先看service,找到服务绑定的具体地址,然后找到service的name;
2、根据name,找到对应的方法,找到方法的参数(入参,出参);
3、根据方法+入参,便知道对应服务的调用方式了,再通过出参去承接对应的返回值即可。

接下来,就是见证奇迹的时刻:

Java怎么写:

服务端

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
29
30
31
32
33
34
35
36
37
/**
*服务端:提供服务
*/

/**
*定义一个自己的webservice接口
*/
import javax.jws.WebService;

/**

* 表示这是一个webservice服务
*/
@WebService
public interface JobService {
public String getJob();
}

/**
*定义接口的实现
*/
import javax.jws.WebService;
/**
*设置服务端点接口 ,指定对外提供服务的接口
*/
@WebService(endpointInterface="cn.it.ws.e.JobService")
public class JobServiceImpl implements JobService {

@Override
public String getJob() {
return "NB工程师";
}
public void say(){
System.out.println("早上好!");
}

}

客户端

1
2
3
4
5
6
7
8
9
10
11
12
import javax.xml.ws.Endpoint;

public class Test {

public static void main(String[] args) {
JobService jobService=new JobServiceImpl();
String address="http://192.168.114.10:9999/ws/jobservice";
Endpoint.publish(address, jobService);
System.out.println("wsdl地址:"+address+"?WSDL");
}

}

注意:

1
2
3
4
5
// 需要注意下面几个点
1、发布以后的头部和尾部,可以增加相关数据校验等;
2、如果要更换soap的版本,可以在@WebService注解后面新增注解:@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING);
3、如果要调用的webservice接口是通过映射到公网上时,需要通过binding修改wsdl中的服务为公网服务,否则无法正常调用;
4、当然也可以正常通过工具直接生成调用代码;

PHP怎么写?

服务端:service.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
class Service
{
public function sayHello($content)
{
return "大家好 ".$content;
}
}
$service = new SoapServer('http://192.168.17.129/soap/Service.wsdl', array('soap_version' => SOAP_1_2));
$service->setClass("Service"); //! 注册Service类的所有方法
$service->handle(); //! 处理请求
?>

服务注册:

1
2
3
4
5
6
7
SoapDiscovery.class.php

<?php
$fso = fopen($this->class_name.".wsdl", "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
fclose($fso);
?>

create_wsdl.php

1
2
3
4
5
6
7
<?php 
include("Service.php");
include("SoapDiscovery.class.php");
//! 第一个参数是类名,生成的wsdl文件就是以它来命名的;第二个参数是服务的名称,可以随便写
$disco = new SoapDiscovery('Service', 'soap');
$disco->getWSDL();
?>

client.php

1
2
3
4
<?php
$soap = new SoapClient('http://192.168.17.129/soap/Service.php?wsdl');
echo $soap->sayHello("如果觉得有用的话,记得点赞,收藏,推荐给身边的同学哈!有什么问题也可与评论区讨论,大家一起进步呀!");
?>

测试输出结果:
大家好 如果觉得有用的话,记得点赞,收藏,推荐给身边的同学哈!有什么问题也可与评论区讨论,大家一起进步呀!