【问题标题】:Logging request/response with CXF dynamic client使用 CXF 动态客户端记录请求/响应
【发布时间】:2013-04-04 12:39:43
【问题描述】:

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

示例代码:

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

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

【问题讨论】:

    标签: xml logging soap cxf interceptor


    【解决方案1】:

    要输出消息,请创建一个像这样的拦截器:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2021-11-15
      相关资源
      最近更新 更多