简体   繁体   中英

How to capture soap error response for an Invalid data in request?

I am able to successfully log request and response for a valid soap request(class generated through wsimport) but couldn't capture the xml content when an exception is thrown(when a node in the request is filled with invalid data). I could get the details of response but I want to capture only the xml part of the raw response. I have tried SOAPFaultException but that gives only the exception message rather than the full envelope with body of the response.How do I capture the exception with only the xml content in the exception/error thrown.

Note: I know I can parse the error(raw response) and pull the xml content but I am wondering if there is simple way/method to get the xml content like below. The content should look like(response captured from Soap UI tool)

<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> 

After exploring more and trying, I found that we need to have a class which should implement SoapHandler interface, this gives you pretty much what was needed. I overrode handleFault method as below.

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;  
    }  

This gave me error info in formatted xml as well as with http headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM