1

我能够成功记录有效soap请求(通过wsimport生成的类)的请求和响应,但是在引发异常时(当请求中的节点填充了无效数据时)无法捕获xml内容。我可以获得响应的详细信息,但我只想捕获原始响应的 xml 部分。我已经尝试过 SOAPFaultException,但它只给出了异常消息,而不是带有响应正文的完整信封。如何在抛出的异常/错误中只捕获 xml 内容的异常。

注意:我知道我可以解析错误(原始响应)并提取 xml 内容,但我想知道是否有简单的方法/方法来获取 xml 内容,如下所示。内容应该看起来像(从 Soap UI 工具捕获的响应)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
   <env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope">
      <wsa:Action>http://schemas.xmlsoap.org/ws/2004/08/addressing/fault</wsa:Action>
      <wsa:MessageID>urn:uuid:xyz</wsa:MessageID>
      <wsa:RelatesTo>urn:uuid:xyz</wsa:RelatesTo>
      <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
   </env:Header>
   <soap:Body>
      <soap:Fault>
         <soap:Code>
            <soap:Value>soap:Sender</soap:Value>
         </soap:Code>
         <soap:Reason>
            <soap:Text xml:lang="en">The 'http://www.fakexyz.com/schemas/xyz:xyz' element is invalid - The value '123' is invalid according to its datatype 'String' - The Pattern constraint failed.
 Please revise your data fields and make sure that message is correctly formatted.</soap:Text>
         </soap:Reason>
         <detail>
            <faulttype>Schema</faulttype>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope> 
4

1 回答 1

0

经过更多探索和尝试,我发现我们需要一个类来实现 SoapHandler 接口,这几乎可以满足您的需求。我覆盖了 handleFault 方法,如下所示。

public boolean handleFault(SOAPMessageContext context) {  
        System.out.println("==============================================RESPONSE(ERROR)=======================================\r\n");  

        String[] abc= context.get(MessageContext.HTTP_RESPONSE_HEADERS).toString().split("],");

        for(String httpheader:abc) {
            System.out.println(httpheader+"],");
        }

        System.out.println("\r\n");

        SOAPMessage message= context.getMessage();                  
        try {                                           
            Source source = message.getSOAPPart().getContent();             
            Transformer transformer = TransformerFactory.newInstance().newTransformer();             
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");                                                 
            transformer.transform(source, new StreamResult(System.out));

        } catch (Exception e) {            
                System.out.println("Response has errors!!");
            }
        return false;  
    }  

这给了我格式化 xml 以及 http 标头的错误信息。

于 2017-12-05T13:27:21.023 回答