【问题标题】:How to capture soap error response for an Invalid data in request?如何捕获请求中无效数据的肥皂错误响应?
【发布时间】:2017-11-27 11:08:04
【问题描述】:

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

【问题讨论】:

    标签: java soap wsimport webservices-client soapexception


    【解决方案1】:

    经过更多的探索和尝试,我发现我们需要一个类来实现 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 标头的错误信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多