| SETUP | Any set up for the following phases(设置阶段) |
| (PRE/USER/POST)_LOGICAL | Processing of objects about to marshalled |
| PREPARE_SEND | Opening of the connection(消息发送准备阶段,在该阶段创建Connection) |
| PRE_STREAM | 流准备阶段 |
| PRE_PROTOCOL | Misc protocol actions(协议准备阶段) |
| WRITE | Writing of the protocol message, such as the SOAP Envelope.(写消息阶段) |
| MARSHAL | Marshalling of the objects |
| (USER/POST)_PROTOCOL | Processing of the protocol message |
| (USER/POST)_STREAM | Processing of the byte level message(字节处理阶段,在该阶段把消息转为字节) |
| SEND | 消息发送 |
在CXF中,所有对消息的处理都是通过各种拦截器实现。CXF已经实现了多种拦截器,如操纵消息头、执行认证检查、验证消息数据、日志记录、消息压缩等,有些拦截器在发布服务、访问服务时已经默认添加到拦截器链。
日志拦截器
CXF已经内置了一些拦截器,这些拦截器大部分默认添加到拦截器链中,有些需要手动添加,如CXF提供的日志拦截器:输入日志拦截器LoggingInInterceptor和输出日志拦截器LoggingOutInterceptor,可以用在服务端也可以用在客户端,用来在测试或调试的时候输出服务端、客户端请求和接收到的信息。
服务端配置方式如下:
jaxrs:inInterceptors
jaxrs:outInterceptors
jaxrs:serviceBeans
API方式:
JAXRSServerFactoryBean jrf = new JAXRSServerFactoryBean();
jrf.setResourceClasses(RestServiceImpl.class);
jrf.setResourceProvider(RestServiceImpl.class, new SingletonResourceProvider(new RestServiceImpl()));
jrf.setAddress(url);
jrf.getInInterceptors().add(new LoggingInInterceptor());
jrf.getOutInterceptors().add(new LoggingOutInterceptor());
jrf.create();
客户端配置方式:
[](https://blog.csdn.net/swazer_z/article/details/72902721
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
“复制代码”)
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=[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("~~~~~~~~~~~~~~~~~~~");
}
}
—
添加到客户端拦截器链:
nge() != null) {
Q.p(message.getExchange().getInMessage());
Q.p(message.getExchange().getOutMessage());
}
Q.p(message.getContent(String.class));
Q.p("~~~~~~~~~~~~~~~~~~~");
}
}
[外链图片转存中…(img-5uZBhkWC-1638958065217)]
—
添加到客户端拦截器链:



