Java JAX-WS Web 服务客户端:如何记录请求和响应 xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2808544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:57:17  来源:igfitidea点击:

Java JAX-WS web-service client: how log request & response xml?

javaweb-servicesjax-ws

提问by EugeneP

I created an implementation of LoggingHandlerthat implements SOAPHandler<SOAPMessageContext>

我创建了一个LoggingHandler实现SOAPHandler<SOAPMessageContext>

It should log whenever handleMessagetriggers (and it is never called in my case)

它应该在handleMessage触发时记录(在我的情况下它永远不会被调用)

MyService service = new MyService();
MyServicePort port = service.getPortType();

now I try this:

现在我试试这个:

BindingProvider bindingProvider = (BindingProvider)port;
bindingProvider.getBinding().getHandlerChain().add(new LoggingHandler());

I do not see any request / response xml though.

虽然我没有看到任何请求/响应 xml。

Can you suggest any solution? Maybe there's another way to see output and request XML?

你能提出任何解决方案吗?也许还有另一种方式来查看输出和请求 XML?

回答by EugeneP

It starts working if you use this method:

如果您使用此方法,它将开始工作:

binding.setHandlerChain(handlerList);

So, first initialize this list with

所以,首先初始化这个列表

binding.getHandlerChain();

then add your element to the list and after all

然后将您的元素添加到列表中,毕竟

setHandlerChain();

回答by LE GALL Beno?t

you can add a logger in you log4j.xml file :

您可以在 log4j.xml 文件中添加记录器:

<!-- Log WebService's inputs and outputs -->
<logger name="org.apache.cxf.interceptor">
    <level value="INFO" />
    <appender-ref ref="[YOUR_LOGGER]" />
</logger>

回答by muttonUp

If anyone is wondering the 'why' of @EugeneP excellent answer.

如果有人想知道@EugeneP 优秀答案的“为什么”。

In the Interface javax.xml.ws.Binding, there is the following comment.

在接口 javax.xml.ws.Binding 中,有如下注释。

/**
 * Gets a copy of the handler chain for a protocol binding instance.
 * If the returned chain is modified a call to 
 <code>setHandlerChain</code>
 * is required to configure the binding instance with the new chain.
 *
 *  @return java.util.List&lt;Handler> Handler chain
 */
 public java.util.List<javax.xml.ws.handler.Handler> getHandlerChain();

So the getHandlerChain() method returns a copy of the List not the list itself. So you have to use setHandlerChain to update the actual List.

因此 getHandlerChain() 方法返回列表的副本而不是列表本身。所以你必须使用 setHandlerChain 来更新实际的 List。