UnmarshallingFailureException, unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault")

ibrahimKiraz picture ibrahimKiraz · Aug 27, 2014 · Viewed 7.4k times · Source

i am trying write a webservice-client for my local webservice. I am debugging (at server) everything is fine and response message is true (my object). And also i try using SOAP UI there was no problem.

Here is my appcontext.xml

  <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"
              p:marshaller-ref="jaxbMarshaller"
              p:checkConnectionForFault="false"
              p:faultMessageResolver-ref="faultMessageResolver"
              p:unmarshaller-ref="jaxbMarshaller"
              p:defaultUri="http://localhost:8080/hesapsorgu/endpoints/sendAccountInformationService.wsdl"
              p:messageSender-ref="messageSender">

            <constructor-arg ref="messageFactory"/>
            <property name="interceptors">
                <list>
                    <ref local="wss4jSecurityInterceptor"/>
                </list>
            </property>
        </bean>

        <bean id="faultMessageResolver" class="tr.com.tnbkep.FaultMessageResolver"/>

<bean id="wss4jSecurityInterceptor"
          class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
        <!-- The web service provider requires us to pass a timestamp,
          username, and password -->
        <property name="securementUsername" value="***" />
        <property name="securementPassword" value="***" />
        <!-- When the web service replies, it will send a timestamp,
          username, and password as well. We want to verify that it is still
          the same provider -->
        <property name="securementPasswordType" value="PasswordText"/>
        <property name="validationActions" value="UsernameToken"/>
        <property name="validationCallbackHandler">
                <!-- just for testing. for real use cases, use integration with spring security -->
                <bean class="org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler">
                    <property name="users">
                        <props>
                            <prop key="ernie">bert</prop>
                        </props>
                    </property>
                </bean>
        </property>
    </bean>

 <bean id="messageSender" class="org.springframework.ws.transport.http.HttpUrlConnectionMessageSender"/>
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>

    <!-- Here we use the Jaxb2 marshaller to marshall and unmarshall our Java objects -->
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

        <property name="classesToBeBound">
            <list>
                <value>tr.com.tnbkep.webservices.AccountInformationResponse</value>
                <value>tr.com.tnbkep.webservices.AccountInformationCorporateRequest</value>
                <value>tr.com.tnbkep.webservices.AccountInformationPersonalRequest</value>
                <value>tr.com.tnbkep.webservices.SendAccountInformationResponse</value>
                <value>tr.com.tnbkep.webservices.SendAccountInformationPersonalRequest</value>
                <value>tr.com.tnbkep.webservices.SendAccountInformationCorporateRequest</value>
            </list>
        </property>
    </bean>

And here is my Main class

public void marshalWithSoapActionHeader(SendAccountInformationPersonalRequest o) throws SOAPException {



        SendAccountInformationResponse sendAccountInformationResponse = (SendAccountInformationResponse) webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {

            public void doWithMessage(WebServiceMessage message) {
                try {
                     SoapMessage soapMessage = (SoapMessage) message;
                     SoapHeader header = soapMessage.getSoapHeader();
                     StringSource headerSource = new StringSource("<wsse:Security   xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">\n" +
                             "         <wsse:UsernameToken wsu:Id=\"UsernameToken-1\">\n" +
                             "            <wsse:Username>user</wsse:Username>\n" +
                             "            <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">***</wsse:Password>        \n" +
                             "         </wsse:UsernameToken>\n" +
                             "      </wsse:Security>");
                     Transformer transformer = TransformerFactory.newInstance().newTransformer();
                     transformer.transform(headerSource, header.getResult());
                     ((SoapMessage) message).getSoapBody().getFault().getFaultDetail().getDetailEntries().next();

                } catch (SoapFaultClientException sfce) {

                    // This indicates there's something wrong with our message
                    // For example a validation error
                    System.out.println(sfce.toString());
                    logger.error("We sent an invalid message", sfce);

                    // Add error to model

                } catch (WebServiceFaultException e) {
                    System.out.println(e.toString());
                    logger.error("We sent an invalid message", e);
                } catch (Exception e) {
                    // exception handling
                }
            }

        });

        System.out.println(sendAccountInformationResponse.getAccountInformationResponse().getMessage());

But when i run my ws-client i am getting this error here is my stacktrace

Exception in thread "main" org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are <{http://tr/com/tnbkep/webservices}SendAccountInformationCorporateRequest>,<{http://tr/com/tnbkep/webservices}SendAccountInformationPersonalRequest>,<{http://tr/com/tnbkep/webservices}SendAccountInformationResponse>
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:863)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:742)
    at org.springframework.ws.support.MarshallingUtils.unmarshal(MarshallingUtils.java:62)
    at org.springframework.ws.client.core.WebServiceTemplate$3.extractData(WebServiceTemplate.java:409)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:598)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:539)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:386)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380)
    at tr.com.tnbkep.SendAndReceive.marshalWithSoapActionHeader(SendAndReceive.java:126)
    at tr.com.tnbkep.SendAndReceive.main(SendAndReceive.java:81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault"). Expected elements are <{http://tr/com/tnbkep/webservices}SendAccountInformationCorporateRequest>,<{http://tr/com/tnbkep/webservices}SendAccountInformationPersonalRequest>,<{http://tr/com/tnbkep/webservices}SendAccountInformationResponse>
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
    at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:229)
    at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:112)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:309)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:292)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:127)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:738)
    ... 13 more

Anybody can help me ? Thank you.

Answer

ibrahimKiraz picture ibrahimKiraz · Sep 1, 2014

i use 2.1.4.RELEASE (spring-ws), I solved this problem. Here is my code.

@Component
public class MyClientInterceptor implements org.springframework.ws.client.support.interceptor.ClientInterceptor {


    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        return false;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
        return true;
    }
}

And added this interceptor to webServiceTemplate. Does work. Thanks all.