Web Service的调用本质:
??? (1)客户端把需要调用的参数,转换为XML文档片段(SOAP消息)。
??? (2)客户端通过网络把XML文档片段传给远程服务器。
??? (3)服务器接受XML文档片段。
??? (4)服务器解析XML文档片段,提取其中的数据,并把数据转换为调用所需的参数。
??? (5)服务器执行方法。
??? (6)得到方法返回值,服务器把方法返回值,转换为XML文档片段(SOAP消息)。
??? (7)服务器通过网络把XML文档片段传给远程客户端。
??? (8)客户端接收XML文档片段。
??? (9)客户端解析XML文档片段,提取其中的数据,并把数据转换为调用返回值。
Web Service的三个技术基础:
??? - WSDL
???????? Web Service接口
???????????? 1.types(标准的Schema)
???????????? 2.2N的message
???????????? 3.portType? -? N个operation
??????? Web Service实现
???????????? 1.binding元素? -? N个更详细的operation
???????????? 2.service????? -? 指定Web Service的服务器地址。
??? - SOAP
???????? Header
???????????? Header是可选的。由程序员控制添加。
???????? Body
???????????? Body元素总是默认的。Body元素里可有两种情况,
???????????? - 当Web Service交互正确时,Body元素里的内容由WSDL控制。
???????????? - 当Web Service交互出错时,Body元素的内容将是Fault子元素。
???????
Web Service急需解决的问题,如何进行权限控制?
?
?解决思路是:服务器端要求input消息总是携带有用户名、密码消息,
--如果没有用户名、密码信息,直接拒绝调用。
如果不用CXF等框架,SOAP消息的生成、解析都是由程序员负责的,
??? 无论是添加用户名、密码信息,还是提取用户名、密码信息,都可有程序员的代码完成。
如果用CXF等框架,SOAP消息的生成、解析都是CXF等框架负责来完成度的。
==================
拦截器
?? 为了让程序员能访问、并修改CXF框架所生成的SOAP消息,CXF提供了拦截器。
?? 服务器端添加拦截器
????? (1)获取Endpoint的publish方法返回值。
????? (2)调用该方法的返回值的getInInterceptor、getOutInterceptor方法来
?????????? 获取In、out拦截器列表,接下来就可以添加拦截器了。
?? 客户端添加拦截器:
????? (1)调用ClientProxy的getClient方法,调用该方法以远程Web Service的代理为参数。
????? (2)调用Client对象的getInInterceptor、getOutInterceptor方法来
?????????? 获取In、Out拦截器列表,接下来就可以添加拦截器了。
对于sayHi操作:
传入消息
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
??????? <S:Body>
?????????? <ns2:sayHi xmlns:ns2="http://ws.cxf.fkjava.org/">
????????????? <text>孙悟空</text>
?????????? </ns2:sayHi>
??????? </S:Body>
</S:Envelope>
传出消息
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
??????? <soap:Body>
??????????????? <ns2:sayHiResponse xmlns:ns2="http://ws.cxf.fkjava.org/">
??????????????????????? <return>孙悟空,您好现在时间是:Thu Jan 03 17:58:39 CST 2013</return>
??????????????? </ns2:sayHiResponse>
??????? </soap:Body>
</soap:Envelope>
对于getAllCats操作:
传入消息
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
??????? <S:Body>
???????????? <ns2:getAllCats xmlns:ns2="http://ws.cxf.fkjava.org/"/>
??????? </S:Body>
</S:Envelope>
传出消息
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
??????? <soap:Body>
??????????????? <ns2:getAllCatsResponse xmlns:ns2="http://ws.cxf.fkjava.org/">
??????????????????????? <return>
????????????????????????????? <entries>
??????????????????????????????????? <key>第4个</key>
??????????????????????????????????? <value>
??????????????????????????????????????? <color>黑白色</color>
??????????????????????????????????????? <id>4</id>
??????????????????????????????????????? <name>熊猫</name>
??????????????????????????????????? </value>
????????????????????????????? </entries>
????????????????????????????? <entries>
??????????????????????????????????? <key>第3个</key>
??????????????????????????????????? <value>
??????????????????????????????????????? <color>白色</color>
??????????????????????????????????????? <id>3</id>
??????????????????????????????????????? <name>kitty</name>
?????????????????????????????????? </value>
???????????????????????????? </entries>
???????????????????????????? <entries>
?????????????????????????????????? <key>第1个</key>
?????????????????????????????????? <value>
?????????????????????????????????????? <color>橙色</color>
?????????????????????????????????????? <id>1</id>
?????????????????????????????????????? <name>garfield</name>
?????????????????????????????????? </value>
???????????????????????????? </entries>
???????????????????????????? <entries>
?????????????????????????????????? <key>第2个</key>
?????????????????????????????????? <value>
?????????????????????????????????????? <color>蓝色</color>
?????????????????????????????????????? <id>2</id>
?????????????????????????????????????? <name>机器猫</name>
?????????????????????????????????? </value>
???????????????????????????? </entries>
?????????????????????? </return>
??????????????? </ns2:getAllCatsResponse>
??????? </soap:Body>
</soap:Envelope>
-------------------------------------------
SOAP消息--个人小补充:
根元素是Envelope
?? Header
????? 默认情况,Header元素不是强制出现的。
????? Header元素有程序员控制添加,主要用于
????? 携带一些额外的信息,比如用户名、密码
????? 信息
?? Body
???? 1.如果调用正确。Body元素的内容应该
???? 遵守WSDL所要求的格式。
???? 2.如果调用错误。Body元素的内容就是
???? Fault子元素。
-------------------------------------------
?
自定义拦截器: ?? ?? 需要实现Interceptor接口。实际上,我们一般会继承AbstractPhaseInterceptor.