Announcement Announcement Module
Collapse
No announcement yet.
SpringWS: Handle SoapFault exception Challenges Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpringWS: Handle SoapFault exception Challenges

    I tried to invoke soap services using webservicetemplate api. I am getting the soap fault error. The desire is to get proper soapfaultclientexception but the JAXB is trying to convert the xml string and throws unmarshalling error. Please advise how can I handle the error gracefully.I want to throw proper exception such as soapfault or custom exception rather than unmarshalling error.
    Code:
    Error Message: 
    org.springframework.oxm.jaxb.JaxbUnmarshallingFailureException: JAXB unmarshalling exception: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are <{http://itkts.etds.ct.aa.com/Request}PingRequest>,<{http://payment.common.ct.aa/operation/ping}PingRequest>,<{http://cs.ct.aa/payment}PingResponse>,<{http://itkts.etds.ct.aa.com/Response}PingResponse>,<{}fault>,<{}ping>; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are <{http://itkts.etds.ct.aa.com/Request}PingRequest>,<{http://payment.common.ct.aa/operation/ping}PingRequest>,<{http://cs.ct.aa/payment}PingResponse>,<{http://itkts.etds.ct.aa.com/Response}PingResponse>,<{}fault>,<{}ping>
    	at org.springframework.oxm.jaxb.JaxbUtils.convertJaxbException(JaxbUtils.java:75)
    	at org.springframework.oxm.jaxb.AbstractJaxbMarshaller.convertJaxbException(AbstractJaxbMarshaller.java:143)
    	at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:425)
    	at org.springframework.ws.support.MarshallingUtils.unmarshal(MarshallingUtils.java:62)
    	at org.springframework.ws.client.core.WebServiceTemplate$3.extractData(WebServiceTemplate.java:374)
    	at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:560)
    	at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:502)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:351)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:345)
    	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:337)

    Code:
    Context.xml:   <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
     
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbMarshaller" />
        <property  name="defaultUri"  value="${aacom.cs.payment.endpoint}"/>
         <property name="faultMessageResolver" ref="faultResolver" />
        </bean>
     <bean id="faultResolver"  class="com.XX.yyy.exception.PaymentFaultResolver"  />
    Code:
    import java.io.IOException;
    
    import javax.xml.transform.dom.DOMSource;
    
    import org.springframework.ws.WebServiceMessage;
    import org.springframework.ws.soap.SoapFault;
    import org.springframework.ws.soap.SoapMessage;
    import org.springframework.ws.client.core.FaultMessageResolver;
    //import org.springframework.ws.soap.client.core.SoapFaultMessageResolver;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class PaymentFaultResolver implements FaultMessageResolver  {
    
    	@Override
    	public void resolveFault(WebServiceMessage message) throws IOException {
    		
    		SoapMessage msg =(SoapMessage)message;
    		System.out.println("msg>>>>>>>>>>>"+msg);
    		
    		String errorMsg = msg.getFaultReason();
    		System.out.println("ERROR>>>>>>>>>>>"+errorMsg);
    		SoapFault sf = msg.getSoapBody().getFault();
    
    		DOMSource s = (DOMSource)sf.getSource();
    
    		DOMSource detailSource = (DOMSource)sf.getFaultDetail().getSource();
    		Node detailNode = detailSource.getNode();
    		NodeList childNodes = detailNode.getChildNodes();
    
    		for (int i = 0; i < childNodes.getLength(); i++) {
    		Node currentNode = childNodes.item(i);
    
    		if (currentNode.getLocalName().equals("NAME_OF_THE_FAULT")) {
    			System.out.println("Fault ERROR>>>>>>>>>>>"+errorMsg);
    		}
    		}
    		
    	}
    }
    I also tried without faultresolver with no luck.
    Thanks
    Ravi

  • #2
    Hi Ravi - Were you able to find a solution to this problem?

    Comment

    Working...
    X