(五) CXF 拦截器--系统拦截器
2016-03-11 10:33
357 查看
【1. 简介】
WS 服务一般需要进行权限限制, 否则的话任何都可以随意调用。 如果使用原生的JDK 发布WS服务,程序员需要对SOAP 消息进行解析,然后进行判断。使用CXF 后,CXF框架可以帮助我们进行SOAP 消息的解析和生成,这样开发起来就更简单了。
WS拦截器分为入拦截器和出拦截器,无论是服务端和客户端都有这两种拦截器。 CXF 提供了一些默认的系统拦截器,如日志拦截器。 此篇文章主要介绍 CXF 提供的日志拦截器。CXF拦截器图:
![](http://img.blog.csdn.net/20160311181056597)
【2. 示例】
【1. 服务器端】 服务器端启动日志入拦截器和日志出拦截器
【2. 客户端】 客户端启动日志入拦截器和日志出拦截器
【3. 请求结果--服务器端输出】
【4. 请求结果--客户端输出】
【5. 总结】
添加CXF系统提供的日志拦截器后,可以清楚地看出调用WS服务的时候发送的参数,包括http请求的相关参数:请求行,请求头,请求体; 当然了还能清楚地看出调用WS接口方法时,发送的SOAP消息体和SOAP 响应的消息体。
WS 服务一般需要进行权限限制, 否则的话任何都可以随意调用。 如果使用原生的JDK 发布WS服务,程序员需要对SOAP 消息进行解析,然后进行判断。使用CXF 后,CXF框架可以帮助我们进行SOAP 消息的解析和生成,这样开发起来就更简单了。
WS拦截器分为入拦截器和出拦截器,无论是服务端和客户端都有这两种拦截器。 CXF 提供了一些默认的系统拦截器,如日志拦截器。 此篇文章主要介绍 CXF 提供的日志拦截器。CXF拦截器图:
【2. 示例】
【1. 服务器端】 服务器端启动日志入拦截器和日志出拦截器
package org.zgf.cxf.test.o2; import java.util.List; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws22.EndpointImpl; import org.apache.cxf.message.Message; import org.zgf.cxf.server.HelloWordWS; /** * 测试 ws 服务器发布 */ public class TestPublishServer { public static void main(String[] args) { //1. 设置发布地址:ip 需要为本机ip, 端口号为本机未被占用的端口号 String address = "http://172.22.12.85:8180/ws/hellowordws"; Endpoint endpoint = Endpoint.publish(address, new HelloWordWS()); EndpointImpl endpointImpl = (EndpointImpl)endpoint; //2. 获取入拦截器列表,默认为null,并添加cxf提供的日志人拦截器 List<Interceptor<? extends Message>> inInterCeptors = endpointImpl.getInInterceptors(); inInterCeptors.add(new LoggingInInterceptor()); //3. 获取出拦截器列表,默认为null,并添加cxf提供的日志出拦截器 List<Interceptor<? extends Message>> outInterCeptors = endpointImpl.getOutInterceptors(); outInterCeptors.add(new LoggingOutInterceptor()); System.out.println("web service 接口发布成功... "); } }
【2. 客户端】 客户端启动日志入拦截器和日志出拦截器
package org.zgf.cxf.test.o2; import java.util.List; import org.apache.cxf.endpoint.Client; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.message.Message; import org.zgf.cxf.client.HelloWordWSService; import org.zgf.cxf.client.IHelloWorldWS; public class Test_client { public static void main(String[] args) { //1. 创建服务代理工厂(继承 javax.xml.ws.Service 的类) HelloWordWSService hwws = new HelloWordWSService(); //2. 创建服务代理 IHelloWorldWS hws = hwws.getHelloWordWSPort(); //3. 获取Client 对象 Client client = ClientProxy.getClient(hws); //4. 获取入参拦截器列表,默认为null,添加cxf 自带的日志入拦截器 List<Interceptor<? extends Message>> inInterCeptors = client.getInInterceptors(); inInterCeptors.add(new LoggingInInterceptor()); //5. 获取出拦截器列表,默认为null,添加cxf自带的日志出拦截器 List<Interceptor<? extends Message>> outInterCeptors = client.getOutInterceptors(); outInterCeptors.add(new LoggingOutInterceptor()); String result = hws.sayByeBye("zong"); System.out.println("result:" + result); } }
【3. 请求结果--服务器端输出】
三月 11, 2016 6:16:23 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass INFO: Creating Service {http://server.cxf.zgf.org/}HelloWordWSService from class org.zgf.cxf.server.IHelloWorldWS 三月 11, 2016 6:16:24 下午 org.apache.cxf.endpoint.ServerImpl initDestination INFO: Setting the server's publish address to be http://172.22.12.85:8180/ws/hellowordws 2016-03-11 18:16:24.184:INFO:oejs.Server:jetty-7.5.4.v20111024 2016-03-11 18:16:24.234:INFO:oejs.AbstractConnector:Started SelectChannelConnector@172.22.12.85:8180 STARTING 2016-03-11 18:16:24.298:INFO:oejsh.ContextHandler:started o.e.j.s.h.ContextHandler{/ws,null} web service 接口发布成功... 三月 11, 2016 6:16:30 下午 org.apache.cxf.services.HelloWordWSService.HelloWordWSPort.IHelloWorldWS INFO: Inbound Message ---------------------------- ID: 1 Address: http://172.22.12.85:8180/ws/hellowordws?wsdl Encoding: UTF-8 Http-Method: GET Content-Type: text/xml Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[text/xml], Host=[172.22.12.85:8180], Pragma=[no-cache], User-Agent=[Apache CXF 2.5.9]} -------------------------------------- 三月 11, 2016 6:16:33 下午 org.apache.cxf.services.HelloWordWSService.HelloWordWSPort.IHelloWorldWS INFO: Inbound Message ---------------------------- ID: 2 Address: http://172.22.12.85:8180/ws/hellowordws Encoding: UTF-8 Http-Method: POST Content-Type: text/xml; charset=UTF-8 Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[196], content-type=[text/xml; charset=UTF-8], Host=[172.22.12.85:8180], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.5.9]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayByeBye xmlns:ns2="http://server.cxf.zgf.org/"><arg0>zong</arg0></ns2:sayByeBye></soap:Body></soap:Envelope> -------------------------------------- 三月 11, 2016 6:16:33 下午 org.apache.cxf.services.HelloWordWSService.HelloWordWSPort.IHelloWorldWS INFO: Outbound Message --------------------------- ID: 2 Encoding: UTF-8 Content-Type: text/xml Headers: {} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayByeByeResponse xmlns:ns1="http://server.cxf.zgf.org/"><return>Bye bye, zong</return></ns1:sayByeByeResponse></soap:Body></soap:Envelope> --------------------------------------
【4. 请求结果--客户端输出】
三月 11, 2016 6:16:31 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL INFO: Creating Service {http://server.cxf.zgf.org/}HelloWordWSService from WSDL: http://172.22.12.85:8180/ws/hellowordws?wsdl 三月 11, 2016 6:16:33 下午 org.apache.cxf.services.HelloWordWSService.HelloWordWSPort.IHelloWorldWS INFO: Outbound Message --------------------------- ID: 1 Address: http://172.22.12.85:8180/ws/hellowordws Encoding: UTF-8 Content-Type: text/xml Headers: {Accept=[*/*], SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayByeBye xmlns:ns2="http://server.cxf.zgf.org/"><arg0>zong</arg0></ns2:sayByeBye></soap:Body></soap:Envelope> -------------------------------------- 三月 11, 2016 6:16:33 下午 org.apache.cxf.services.HelloWordWSService.HelloWordWSPort.IHelloWorldWS INFO: Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {Content-Length=[225], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayByeByeResponse xmlns:ns1="http://server.cxf.zgf.org/"><return>Bye bye, zong</return></ns1:sayByeByeResponse></soap:Body></soap:Envelope> -------------------------------------- result:Bye bye, zong<span style="font-size:14px;"><strong> </strong></span>
【5. 总结】
添加CXF系统提供的日志拦截器后,可以清楚地看出调用WS服务的时候发送的参数,包括http请求的相关参数:请求行,请求头,请求体; 当然了还能清楚地看出调用WS接口方法时,发送的SOAP消息体和SOAP 响应的消息体。
相关文章推荐
- [LeetCode]145. Binary Tree Postorder Traversal
- [magento]Warning: Your Magento folder does not have sufficient write permissions. If you wish to pro
- php 中curl获取用户文件的大小
- nginx配置
- LeetCode:Palindrome Number
- C#字符串压缩与解压
- 3.09课·········for穷举和迭代
- 关于搭建微信本地调试环境的说明
- failed to find target with hash string 'android-23'
- iOS面试题三
- 定制WES7紧急修复U盘
- Storyboard介绍及使用2 Storyboards Part2 译文
- (二) CXF 发布WS服务
- (三) CXF 客户端调用WS服务
- javascript中top、clientTop、scrollTop、offsetTop的讲解
- Windows环境下CGAL的安装
- Java Job
- C#将exe运行程序嵌入到自己的winform窗体中
- 物体的惯性旋转
- 暗通道图像去雾