gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:32:16 25 4
gpt4 key购买 nike

我正在尝试向 wsdl2java 的入站/出站流量添加一些日志记录生成的客户端。
我有客户端生成并使用它如下:

伪代码:

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

有什么办法可以添加一些拦截器吗? 我在 中使用它 Spring 顺便申请!

最佳答案

您可以实现自己的 javax.xml.ws.handler.soap.SOAPHandler如果您需要以不同的方式自定义日志记录或处理 SOAP 消息,方法如下:

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

关于web-services - 如何将拦截器添加到 wsdl2java (CXF) 生成的客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com