我正在使用CXF动态客户端并希望进行一些日志记录。我想记录传入和传出的soap xml。我需要使用拦截器做到这一点,但不知道如何连接它们,以便客户端使用它。
示例代码:
MyWebservice ws = new MyWebservice_Service().getChargePointServiceSoap12();
如何向此客户端添加拦截器?
答案 0 :(得分:3)
结果消息创建一个这样的拦截器:
import java.io.InputStream;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
public class LogInterceptor extends AbstractPhaseInterceptor<Message> {
public LogInterceptor() {
super(Phase.PRE_INVOKE);
}
@Override
public void handleMessage(Message message) throws Fault {
InputStream contentInputStream = message.get(InputStream.class);
// Do whatever you want with the content (the soap xml)
}
public void addInterceptor(Object portType) {
Client client = ClientProxy.getClient(portType);
client.getInInterceptors().add(this);
}
}
您的代码应该调用addInterceptor:
MyWebservice ws = new MyWebservice_Service().getChargePointServiceSoap12();
LogInterceptor logInterceptor = new LogInterceptor();
logInterceptor.addInterceptor(ws);
最后,对于收入消息,您可以更改拦截器构造函数的阶段。