2014-08-27 146 views
0

我正在嘗試爲我的本地webservice編寫webservice-client。我正在調試(在服務器)一切都很好,響應信息是真實的(我的對象)。而且我嘗試使用SOAP UI沒有問題。UnmarshallingFailureException,意外的元素(uri:「http://schemas.xmlsoap.org/soap/envelope/」,local:「Fault」)

這裏是我的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> 

這裏是我的主類

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()); 

但是當我運行我的WS-客戶端我在這裏得到這個錯誤是我的堆棧跟蹤

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 

有人可以幫助我嗎?謝謝。

+0

您正在使用哪個版本的Spring-WS?沒有版本號的堆棧跟蹤中的行號是沒用的... – 2014-08-29 10:25:02

+0

我認爲你需要退後一步。您似乎添加了幾個令人困惑的圖層。首先,你可以刪除所有的Spring WS安全內容。另外,刪除像p:checkConnectionForFault =「false」這樣的定製。相反,我們想要檢查缺省情況,就像Spring WS默認的那樣。如果存在規範級別的SOAP錯誤,我們希望看到它。你也有p:faultMessageResolver-ref =「faultMessageResolver」。我建議刪除它並使用Spring WS的默認錯誤處理程序來減少移動部件的數量。隨着這些變化,回來後張貼結果。 – gregturn 2014-08-29 15:11:30

回答

2

我使用2.1.4.RELEASE(spring-ws),我解決了這個問題。這是我的代碼。

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

並將此攔截器添加到webServiceTemplate中。工作。 謝謝大家。

相關問題