简体   繁体   English

如何将拦截器添加到 wsdl2java (CXF) 生成的客户端?

[英]How to add interceptors to a wsdl2java (CXF) generated client?

I am trying to add some logging to the inbound / outbound traffic of a wsdl2java generated client.我正在尝试向wsdl2java生成的客户端的入站/出站流量添加一些日志记录。 I have the client generated and using it as follows:我有客户端生成并使用它如下:

Pseudo code:伪代码:

MyService ws = new MyService().getMyServiceSoap12();
((BindingProvider)ws).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, webServiceAddress); // for dynamic endpoints...

Is there any way I can add some interceptors?有什么办法可以添加一些拦截器吗? I am using it in a Spring application by the way!顺便说一下,我在Spring应用程序中使用它!

You can implement your own javax.xml.ws.handler.soap.SOAPHandler if you need customized logging or handling SOAP messages in a different way, here is how:如果您需要自定义日志记录或以不同方式处理 SOAP 消息,您可以实现自己的javax.xml.ws.handler.soap.SOAPHandler ,方法如下:

MyService ws = new MyService().getMyServiceSoap12();

BindingProvider wsBindingProvider = (BindingProvider) ws;

// Get a copy of the handler chain for a protocol binding instance
List<Handler> handlers =
    wsBindingProvider.getBinding().getHandlerChain();

// Add your handler(s)
handlers.add(new MySoapMessageLogger());

// We need to setHandlerChain because setHandlerChain
// returns a copy of List<Handler>
wsBindingProvider.getBinding().setHandlerChain(handlers);

The MySoapMessageLogger may look like this: MySoapMessageLogger 可能如下所示:

public class MySoapMessageLogger implements SOAPHandler<SOAPMessageContext> {

    private static final Logger logger = 
        LoggerFactory.getLogger(MySoapMessageLogger.class);

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        // Retrieve the message contents
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        context.getMessage().writeTo(stream);
        
        // You've got your XML message and can log it right away or
        // beautify it with some library before sending to log
        logger.trace((isRequest ? "Request:" : "Response:") + stream.toString());

        return true;
    }

    /* For the logging purposes the following methods can be leaved as stubs */

    @Override
    public Set<QName> getHeaders() {
        return null;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }

    @Override
    public void close(MessageContext context) {
    }
}

I am not sure if your question is asking how to enable the built-in request/response logging, or rather replace and/or enhance existing logging facilities.我不确定您的问题是问如何启用内置的请求/响应日志记录,还是替换和/或增强现有的日志记录工具。

Assuming the former, I suggest looking at Debugging and Logging and Configuration sections of the CXF User Guide .假设是前者,我建议查看CXF User Guide调试、日志记录配置部分。 The most important point is that CXF uses Java SE logging by default which means you'll need to toss a SLF4J bridge in your project if you want to use something else.最重要的一点是 CXF 默认使用Java SE 日志记录,这意味着如果你想使用其他东西,你需要在项目中扔一个 SLF4J 桥。

To enable logging merge these bits (note cxf namespace) in your Spring configuration:要在 Spring 配置中启用日志合并这些位(注意cxf命名空间):

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus> 
</beans> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM