java 打印 SOAP 消息的 XML 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3969399/
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-30 04:12:01  来源:igfitidea点击:

Print XML content of SOAP message

javaweb-servicessoapcxf

提问by tobiasbayer

I am using Apache CXF for my webservices. I've created an instance of AbstractSoapInterceptor. In its public void handleMessage(SoapMessage message) throws Faultmethod I would like to print the XML content of the intercepted message to the console. How can I achieve that?

我正在为我的网络服务使用 Apache CXF。我已经创建了一个AbstractSoapInterceptor. 在它的public void handleMessage(SoapMessage message) throws Fault方法中,我想将截获消息的 XML 内容打印到控制台。我怎样才能做到这一点?

回答by Aaron McIver

Check thisout and search for INBOUND INTERCEPTOR. Will place it here for reference...

检查这个出去找入境的拦截器。将其放在这里以供参考...

public class InterceptorMensajeSOAPIn extends AbstractSoapInterceptor {

      private static Logger log =
Logger.getLogger(InterceptorMensajeSOAPIn.class);



      private SAAJInInterceptor saajIn = new SAAJInInterceptor();

      public InterceptorMensajeSOAPIn(){

            super(Phase.PRE_PROTOCOL);

            getAfter().add(SAAJInInterceptor.class.getName());

      } 


      public void handleMessage(SoapMessage message) throws Fault {

        SOAPMessage soapMessage = getSOAPMessage(message);

        try {

                  soapMessage.writeTo(System.out);

            } catch (Exception e) {

                  e.printStackTrace();

            }
      }


      private SOAPMessage getSOAPMessage(SoapMessage smsg){

            SOAPMessage soapMessage = smsg.getContent(SOAPMessage.class);

        if (soapMessage == null) {

            saajIn.handleMessage(smsg);

            soapMessage = smsg.getContent(SOAPMessage.class);

        }   

        return soapMessage;

      }
}

回答by Daniel Kulp

Any reason you cannot just use the LoggingInInterceptorthat is shipped with CXF? You could just grab the code for that and use that as a basis, but in 2.3, the LoggingInInterceptorwas enhanced to allow specifying a printstream and such to use, so it might "just work".

有什么理由不能只使用LoggingInInterceptorCXF 附带的?您可以只获取代码并将其用作基础,但在 2.3 中,LoggingInInterceptor增强了允许指定打印流等使用,因此它可能“正常工作”。

回答by Stijn Geukens

You can also use a feature for this: org.apache.cxf.feature.LoggingFeature:

您还可以为此使用一个功能org.apache.cxf.feature.LoggingFeature

<jaxws:endpoint ...>
    <jaxws:features>
        <bean class="org.apache.cxf.feature.LoggingFeature"/>
    </jaxws:features>
</jaxws:endpoint>