Web Service调用本质
1、客户端把需要调用的参数,转换为XML文档片段(SOAP消息)
2、客户端通过网络把XML文档片段传给远程服务器
3、服务器接受XML文档片段
4、服务器解析XML文档片段,提取其中的数据,并把数据转换为调用所需要的参数
5、服务器执行方法
6、得到方法返回值,服务器把方法返回值转换为XML文档片段(SOAP消息)
7、服务器通过网络把XML文档段传给远程客户端
8、客户端接收XML文档片段
9、客户端解析XML片段,提取其中的数据,并把数据转换为返回值
CXF在服务端添加拦截器的用法:
//发布Web Service
EndpointImpl ep = (EndpointImpl) Endpoint.publish("http://127.0.0.1:9998/onyasWS", hw);
//添加In拦截器
ep.getInInterceptors().add(new LoggingInInterceptor());
//添加Out拦截器,这时就会在控制台上打印出日志信息
ep.getOutInterceptors().add(new LoggingOutInterceptor());
这时重新生成客户端,然后运行客户端代码,可以发现控制台上生成了许多日志信息。
如下所示为其中一个片段:
2014-5-2 20:02:54 org.apache.cxf.services.HelloWorldWs.HelloWorldWsPort.HelloWorld
信息: Inbound Message
----------------------------
ID: 13
Address: http://localhost:9998/onyasWS
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2,
*/*; q=.2], connection=[keep-alive], Content-Length=[256], content-type=[text/xml;
charset=UTF-8], Host=[localhost:9998], SOAPAction=[""], User-Agent=[Java/1.6.0_13]}
Payload:
<?xml version="1.0" ?>
<S:Envelopexmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getCatsByUser xmlns:ns2="http://cxf.ws.onyas.com/">
<arg0>
<password>123</password>
<username>小明</username>
</arg0>
</ns2:getCatsByUser>
</S:Body>
</S:Envelope>
--------------------------------------
2014-5-2 20:02:54 org.apache.cxf.services.HelloWorldWs.HelloWorldWsPort.HelloWorld
信息: Outbound Message
---------------------------
ID: 13
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getCatsByUserResponse xmlns:ns2="http://cxf.ws.onyas.com/">
<return>
<color>红色</color>
<id>1</id>
<name>红眼猫</name>
</return>
<return>
<color>蓝色</color>
<id>2</id>
<name>机器</name>
</return>
</ns2:getCatsByUserResponse>
</soap:Body>
</soap:Envelope>
下一篇文章将详细介绍这个日志的内容