客户端配置方式:
JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean();
factory.setServiceClass(RestService.class);
factory.setAddress(url);
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
RestService ser = factory.create(RestService.class);
ser.get();
输出日志形式如下:客户端:
[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/webapp/ws/cxf/rest
Http-Method: GET
Content-Type: application/xml
Headers: {Content-Type=[application/xml], Accept=[text/plain]}
--------------------------------------
[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: ISO-8859-1
Content-Type: text/plain
Headers: {content-type=[text/plain], Date=[Wed, 22 Mar 2017 14:33:39 GMT], transfer-encoding=[chunked]}
Payload: this is default get plain
--------------------------------------
服务端:
[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Inbound Message
----------------------------
ID: 4
Address: http://localhost:8080/webapp/ws/cxf/rest
Encoding: UTF-8
Http-Method: GET
Content-Type: application/xml
Headers: {Accept=[text/plain], cache-control=[no-cache], connection=[keep-alive], content-type=[application/xml], host=[localhost:8080], pragma 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 =[no-cache], user-agent=[Apache CXF 3.0.3]}
--------------------------------------
[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Outbound Message
---------------------------
ID: 4
Response-Code: 200
Content-Type: text/plain
Headers: {Content-Type=[text/plain], Date=[Wed, 22 Mar 2017 14:33:39 GMT]}
Payload: this is default get plain
--------------------------------------
自定义拦截器
CXF中也可以自定义拦截器,CXF中实现自定义拦截器需要继承AbstractPhaseInterceptor或者其子类如AbstractSoapInterceptor。
一个简单的拦截器:
public class MyInterceptor extends AbstractPhaseInterceptor{
//必需提供一个带参数的构造函数
public MyInterceptor(String phase){
super(phase);
}
//覆写拦截后的动作
@Override
public void handleMessage(Message message) throws Fault{
Q.p(“~~~~~~~~~~~~~~~~~~”);
if (message.getDestination() != null) {
Q.p(message.getDestination().getAddress());
}
if (message.getExchange() != null) {
Q.p(message.getExchange().getInMessage());
Q.p(message.getExchange().getOutMessage());
}
Q.p(message.getContent(String.class));
Q.p(“~~~~~~~~~~~~~~~~~~~”);
}
}
—
添加到客户端拦截器链:
JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean();
factory.setServiceClass(RestService.class);
factory.setAddress(url);
factory.getInInterceptors().add(new MyInterceptor(Phase.RECEIVE));
factory.getOutInterceptors().add(new MyInterceptor(Phase.SEND));
ser = factory.create(RestService.class);
ser.get();
—
输出:
[DEBUG] 03-22 23:07:38 org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:304) Invoking handleMessage on interceptor cn.lg.ws.MyInterceptor@58cbafc2
~~~~~~~~~~~~~~~~~~
null
{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Content-Type=[application/xml], Accept=[text/plain]}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.URLConnectionHTTPConduit1715248762target: http://localhost:8080/webapp/ws/cxf/rest, org.apache.cxf.request.uri=http://localhost:8080/webapp/ws/cxf/rest, jaxrs.template.parameters=null, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:8080/webapp/ws/cxf/rest, jaxrs.proxy=true, org.apache.cxf.request.method=GET, org.apache.cxf.transport.processOneWayResponse=true, http.connection=sun.net.www.protocol.http.HttpURLConnection:http://localhost:8080/webapp/ws/cxf/rest, org.apache.cxf.message.Message.BASE_PATH=http://localhost:8080/webapp/ws/cxf/, org.apache.cxf.client=true, org.apache.cxf.invocation.context={ResponseContext={}, RequestContext={org.apache.cxf.message.Message.PROTOCOL_HEADERS={Content-Type=[application/xml], Accept=[text/plain]}, org.apache.cxf.request.uri=http://localhost:8080/webapp/ws/cxf/rest, BODY_INDEX=-1, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:8080/webapp/ws/cxf/rest, jaxrs.proxy=true, org.apache.cxf.jaxrs.model.OperationResourceInfo=org.apache.cxf.jaxrs.model.OperationResourceInfo@117159c0}}, java.lang.annotation.Annotation=[Ljava.lang.annotation.Annotation;@19e4653c, org.apache.cxf.message.inbound=false, http.scheme=http, Content-Type=application/xml, org.apache.cxf.empty.request=true}
null
[](https://blog.csdn.net/swazer_z/article/details/72902721 "复制代码") --- [](https://blog.csdn.net/swazer_z/article/details/72902721 "复制代码") xf.empty.request=true} null
[外链图片转存中…(img-I9oRA3NY-1652164522666)]
—
[外链图片转存中…(img-J8vrU6Xw-1652164522666)]



