0

我正在使用 CXF 动态客户端并想做一些日志记录。我想记录传入和传出的soap xml。我需要使用拦截器来做到这一点,但不知道如何连接它们,以便客户端使用它。

示例代码:

    MyWebservice ws = new MyWebservice_Service().getChargePointServiceSoap12();

如何向此客户端添加拦截器?

4

1 回答 1

4

要输出消息,请创建一个拦截器,如下所示:

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);

最后,对于收入消息,您更改拦截器构造函数的阶段。

文档:http ://cxf.apache.org/docs/interceptors.html

于 2013-04-05T02:41:01.647 回答