【问题标题】:Java - How to show Soap FaultJava - 如何显示肥皂故障
【发布时间】:2014-09-10 17:37:06
【问题描述】:

当 Soap 故障调用 Java Web 服务时,我有这种响应

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Server</faultcode>
            <faultstring>Fault occurred while processing.</faultstring>
            <detail>
                <ns1:WaybillRegistrationFault xmlns:ns1="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                    <errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                        <code>80000</code>
                        <description>El número de CTG 20140904 ya existe</description>
                    </errors>
                    <errors xmlns:ns2="http://pod.waybillmanagement.ws.industrysystem.com.ar/">
                        <code>1000</code>
                        <description>La carta de porte ya se encuentra registrada.</description>
                    </errors>
                </ns1:WaybillRegistrationFault>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

我用它的 handleFault 方法做了一个肥皂处理程序,如下所示: public boolean handleFault(SOAPMessageContext context) {

    try {
        System.err.println("Handler handleFault");

        if (context.getMessage().getSOAPBody().hasFault()) {
            SOAPFault fa = context.getMessage().getSOAPBody().getFault();
            System.err.println(fa.getFaultString());
            System.err.println("FaultCode: " + fa.getFaultCode() + " - Detail: " + fa.getDetail());
        }
        return true;
    } catch (SOAPException ex) {
        System.err.println("SoapEx " + ex.getMessage());
        return true;
    }
}

但在我的输出中,我只有:

Handler handleFault
Fault occurred while processing.
FaultCode: soap:Server - Detail: [detail: null]

如何处理错误节点?

更新:

fa.getDetail().getFirstChild().getTextContent() 我得到了 xml 中的文本。我如何将其作为对象。我认为这是一个 WaybillRegistrationFault。

【问题讨论】:

    标签: java web-services netbeans jax-ws soapfault


    【解决方案1】:

    这是我处理soap错误错误的方式:

    StringWriter sw = new StringWriter();
    TransformerFactory.newInstance().newTransformer().transform(
    new DOMSource(soapFaultException.getFault()), new StreamResult(sw));
    String xml = sw.toString();
    

    【讨论】:

      【解决方案2】:

      如果要将故障内容打印为xml字符串,必须将fa.getDetail()返回的dom实例转换为String,fa.getDetail().toString()返回["+getNodeName()+" : "+getNodeValue()+"]" 不是xml的内容。

      尝试以下方法;

      String detailStr=dom2string(fa.getDetail())
      System.err.println("FaultCode: " + fa.getFaultCode() + " - Detail: " + detailStr); 
      
      
      public static final String dom2string(Node node) {
          try {
              if (node == null) {
                  return null;
              }
      
              Transformer tf = transformerThreadLocal.get();
              // Create writer
              StringWriter sw = new StringWriter(buffer, Integer.MAX_VALUE);
              StreamResult result = new StreamResult(sw);
      
              // transform
              tf.transform(new DOMSource(node), result);
      
              return sw.getBuffer();
          } catch (Exception e) {
              throw new RuntimeException("Could not convert Node to string", e);
          }
      }
      

      【讨论】:

      • 执行fa.getDetail().getFirstChild().getTextContent() 我收到了消息,但都是文本格式,有没有办法将它作为一个对象?
      • fa.getDetail().getFirstChild() 是你想要的对象
      • 值得注意的是,它不是Java对象,而是XML类的对象,我必须通过Node和NodeList来解析。
      • 如果您更喜欢将 xml 文档作为对象而不是 dom 来处理,我强烈建议您使用 xmlbeans。检查xmlbeans.apache.org
      猜你喜欢
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      相关资源
      最近更新 更多