(五) CXF 拦截器--系统拦截器

来源:互联网 发布:线性优化模型 编辑:程序博客网 时间:2024/05/16 19:43

【1. 简介】

     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 buildServiceFromClassINFO: 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 initDestinationINFO: Setting the server's publish address to be http://172.22.12.85:8180/ws/hellowordws2016-03-11 18:16:24.184:INFO:oejs.Server:jetty-7.5.4.v201110242016-03-11 18:16:24.234:INFO:oejs.AbstractConnector:Started SelectChannelConnector@172.22.12.85:8180 STARTING2016-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.IHelloWorldWSINFO: Inbound Message----------------------------ID: 1Address: http://172.22.12.85:8180/ws/hellowordws?wsdlEncoding: UTF-8Http-Method: GETContent-Type: text/xmlHeaders: {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.IHelloWorldWSINFO: Inbound Message----------------------------ID: 2Address: http://172.22.12.85:8180/ws/hellowordwsEncoding: UTF-8Http-Method: POSTContent-Type: text/xml; charset=UTF-8Headers: {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.IHelloWorldWSINFO: Outbound Message---------------------------ID: 2Encoding: UTF-8Content-Type: text/xmlHeaders: {}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 buildServiceFromWSDLINFO: 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.IHelloWorldWSINFO: Outbound Message---------------------------ID: 1Address: http://172.22.12.85:8180/ws/hellowordwsEncoding: UTF-8Content-Type: text/xmlHeaders: {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.IHelloWorldWSINFO: Inbound Message----------------------------ID: 1Response-Code: 200Encoding: UTF-8Content-Type: text/xml;charset=UTF-8Headers: {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 响应的消息体。







0 0