为什么在调用Soap Web服务时出现javax.xml.bind.UnmarshalException

时间:2019-05-08 19:45:06

标签: soap-client spring-ws

我正在尝试使用SOAP Web服务,并且已经使用Spring-ws编写了SOAP客户端。这还包括消息签名。但是,当我尝试触发请求时,出现异常。从服务器的角度来看,服务器正在发送成功的响应,但我无法对其进行编组。

我遵循了很多建议,包括一个建议 UnmarshallingFailureException, unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Fault")

但是,按照上面的链接删除了异常,但是我得到了一个空响应对象,并且我无法找出响应丢失的地方。

这是我的Config类方法:

    public WebServiceTemplate accountsInquiryWebServiceTemplate() throws Exception {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        Jaxb2Marshaller accountsInquiryMarshaller = accountsInquiryMarshaller();
        webServiceTemplate.setMarshaller(accountsInquiryMarshaller);
        webServiceTemplate.setUnmarshaller(accountsInquiryMarshaller);
        ClientInterceptor[] clientInterceptors = new ClientInterceptor[1];
        clientInterceptors[0] = wsClientSecurityInterceptor();
        webServiceTemplate.setInterceptors(clientInterceptors);
        webServiceTemplate.setCheckConnectionForFault(true);
        webServiceTemplate.setDefaultUri(serviceURL);

        return webServiceTemplate;
    }
private Jaxb2Marshaller accountsInquiryMarshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPaths(new String[]{
             "com.accounting.integrationservice"
});
        return jaxb2Marshaller;
    }
private Wss4jSecurityInterceptor wsClientSecurityInterceptor() throws Exception {
        Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
        wss4jSecurityInterceptor.setSecurementActions("Signature");
        wss4jSecurityInterceptor.setSecurementUsername(username);
        wss4jSecurityInterceptor.setSecurementPassword(password);
        wss4jSecurityInterceptor.setSecurementSignatureCrypto(clientCrypto());
        wss4jSecurityInterceptor.setValidationActions("Signature");

        return wss4jSecurityInterceptor;
    }
private Crypto clientCrypto() throws Exception {
        CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();
        cryptoFactoryBean.setKeyStoreLocation(resourceLoader.getResource("classpath:accountsInquiry/keystore/" + keyStoreLocation));
        cryptoFactoryBean.setKeyStorePassword(keyStorePassword);
        cryptoFactoryBean.afterPropertiesSet();
        return cryptoFactoryBean.getObject();
    }

客户致电如下:

Response response = accountsWebServiceTemplate.marshalSendAndReceive(request);

我得到的异常堆栈如下:它与上面引用的链接类似,因此不要放置整个异常堆栈

org.springframework.ws.soap.security.AbstractWsSecurityInterceptor.handleResponse(AbstractWsSecurityInterceptor.java:247)
 - Could not validate request: No WS-Security header found
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://www.qualityaccounts.com/IntegrationService/schemas/ProductInfo/v1}
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:911)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:784)
    at org.springframework.ws.support.MarshallingUtils.unmarshal(MarshallingUtils.java:62)
    at org.springframework.ws.client.core.WebServiceTemplate$3.extractData(WebServiceTemplate.java:413)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:619)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383)

1 个答案:

答案 0 :(得分:0)

好吧,即使没有标题,我也尝试使用“签名”来验证响应的安全标题。但是,如果我没有指定ValidationAction,它会抛出NullPointerException,因此它期望至少一个ValidationAction。

为了解决所有这些问题,我告诉Spring安全性不要通过这段代码来验证响应。

wss4jSecurityInterceptor.setValidateResponse(false);

感谢这篇文章 Spring Security - Wss4jSecurityInterceptor - Nullpointer