如何从catch SoapFaultClientException块中的SOAP WS响应中提取自定义的Exception

时间:2018-06-04 19:54:25

标签: java xml web-services soap

我有客户端SOAP Web服务,它返回值或自定义异常作为响应。 WS客户端是使用JDK wsimport实用程序从WSDL生成的。

如果WS失败,工厂中有方法获取错误类。

    /**
 * Create an instance of {@link ConvertFault }
 * 
 */
public ConvertFault createConvertFault() {
    return new ConvertFault();
}

/**
 * Create an instance of {@link JAXBElement }{@code <}{@link ConvertFault
 * }{@code >}}
 * 
 */
@XmlElementDecl(namespace = "http://convertorws.company.cz/", name = "ConvertorWSFault")
public JAXBElement<ConvertFault> createConvertorWSFault(ConvertFault value) {
    return new JAXBElement<ConvertFault>(_ConvertorWSFault_QNAME, ConvertFault.class, null, value);
}

获取响应和捕获异常的客户端代码如下所示:

            @SuppressWarnings("unchecked")
        public Pair<String, String> idFnDownloadFile(long id, boolean wait) throws Exception {
            IdFNDownloadFile request = new IdFNDownloadFile();
            request.setCorrelation(id);
            request.setWait(wait);
            JAXBElement<IdFNDownloadFileResponse> response = null;
            JAXBElement<ConvertException> userElementEx = null;

            try {
                response = (JAXBElement<IdFNDownloadFileResponse>) getWebServiceTemplate()
                        .marshalSendAndReceive(new ObjectFactory().createIdFNDownloadFile(request));
            } catch (SoapFaultClientException soapEx) {
    // Doesn't work
                JAXBContext jaxbContext = JAXBContext.newInstance(ConvertException.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

                userElementEx = jaxbUnmarshaller.unmarshal(soapEx.getSoapFault().getSource(), ConvertException.class);
                ConvertException convertException = userElementEx.getValue();
                System.out.println(convertException.toString());...

如果我调试异常,一切看起来都很好:

<?xml version='1.0' encoding='utf-8'?>
        <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
            <S:Body>
                <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
                    <faultcode>S:Server</faultcode>
                    <faultstring>There is no conversion correlated with given correlation: 1528116806060</faultstring>
                    <detail>
                        <ns2:ConvertorWSFault xmlns:ns2="http://convertorws.company.cz/">
                        <faultCode>321</faultCode>
                        <faultString>There is no conversion correlated with given correlation: 1528116806060</faultString>
                        </ns2:ConvertorWSFault>
                    </detail>
                </S:Fault>
            </S:Body>
</S:Envelope>

我应该如何找出异常块来获取异常中的ConvertFault类?

0 个答案:

没有答案