我的WebService学习第三课 - wsdl and soap

紧接着上节课,这节课的主要内容有3部分:
1. WSDL的介绍

当我们在server端开启服务之后,在浏览器中输入WSDL的地址,如http://localhost:9999/ns?wsdl,会访问到一个页面,那这个页面是什么呢?
它就是我们发布的服务对外提供的接口描述,总共包含以下5个组成:

<types/>
<message name="add">
<message name="addResponse">
<message name="minus">
<message name="minusResponse">
<portType name="IMyService">--
<binding name="MyServiceImplPortBinding" type="tns:IMyService">
<service name="MyServiceImplService">
每一条都对应具体的格式,这里不过多描述,主要给出它们的功能:
types : 定义访问的类型(数据,方法)
message : 定义发送、接收的soap消息参数
portType : 指明服务器的接口,并通过operation绑定相应的in和out消息,其中in表示参数,out表示返回值
binding : 指定传递消息所使用的格式
service : 指定服务所发布的名称
这里其实我们可以根据自己写的demo对应着看这个页面,会有更好的理解。


2. SOAP的介绍
SOAP : Simple Object Access Protocol,简单对象传输协议
webservice就是通过SOAP消息来传递我们的请求和回复,实现client和server之间的交互。关于SOAP的更多详细介绍,大家可以百度一下,这里我们只要知道它的作用就够了。

可以有很多的方式来查看我们发的SOAP消息到底长什么样子,eclipse也有自带的工具,不错这里介绍一个简单实用的:TCPMon


3. TCPMon的使用
TCPMon的原理很简单,可以参照附件,其实就是一个中间截获的概念。
不过这时候TestClient里的URL端口就要相应的改成TCPMon的监听端口了。
最后,当再一次运行client方法时,我们会有如下的截获的消息:
request
<?xml version="1.0" ?>
   <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
      <S:Body>
         <ns2:add xmlns:ns2="http://service.pintn.www/">
            <a>17</a>
            <b>13</b>
         </ns2:add>
      </S:Body>
   </S:Envelope>

response
<?xml version="1.0" ?>
            <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
               <S:Body>
                  <ns2:addResponse xmlns:ns2="http://service.pintn.www/">
                     <addResult>30</addResult>
                  </ns2:addResponse>
               </S:Body>
            </S:Envelope>
关于如何使用TCPMon,可以参考附件截图。

从这个消息里面, 我们发现为什么输入的参数会变成<a>、<b>,输出的会变成<addResult>呢?
原因是这几个参数也是有方法修改的,同样是要加annotation:
server端
IMyService.java
@WebResult(name="addResult")
public int add(@WebParam(name="a")int a, @WebParam(name="b")int b);

@WebResult(name="minusResult")
public int minus(@WebParam(name="a")int a, @WebParam(name="b")int b);
现在大家明白了吧?

猜你喜欢

转载自dongqiang1989-126-com.iteye.com/blog/2002918