首页 > 解决方案 > SOAP 错误无法解组

问题描述

我的问题是 SOAP 错误中的值没有正确解组。以下是 SOAP FAULT 字符串:

Source faultPayload = new StringSource("<SOAP-ENV:Fault xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://domain/schema/common/fault\">"
                + "<faultcode>SOAP-ENV:Server</faultcode>"
                + "<faultstring xml:lang=\"en\">fault</faultstring>"
                + "<detail>"
                + "<v1:ExceptionInfoType>"
                + "<v1:TransactionId>655655455</v1:TransactionId>"
                + "<v1:ErrorCode>4</v1:ErrorCode>"
                + "<v1:Description>bla</v1:Description>"
                + "</v1:ExceptionInfoType>"
                + "</detail>"
                + "</SOAP-ENV:Fault>");

这是解组代码,但对象exceptionInfo (ExceptionInfoType) 没有正确映射的值/属性:

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
SoapFaultDetail faultDetail = saajSoapMessage.getSoapBody().getFault().getFaultDetail();
Iterator<SoapFaultDetailElement> iterator = faultDetail.getDetailEntries();
if (iterator.hasNext()) {
     SoapFaultDetailElement detailElement = iterator.next();
     JAXBElement<ExceptionInfoType> exceptionElement = unmarshaller.unmarshal(detailElement.getSource(),ExceptionInfoType.class);
     ExceptionInfoType exceptionInfo = exceptionElement.getValue();
     ThreadContext.put("transactionId", String.valueOf(exceptionInfo.getTransactionId()));
     throw new IOException(BigExceptionHelper.createException(exceptionInfo.getErrorCode(),
                        exceptionInfo.getDescription()));
}

下面是对象的类,肥皂应该在其中解组,但它失败了,或者更准确地说,属性值没有设置:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ExceptionInfoType", propOrder = {
    "transactionId",
    "errorCode",
    "description"
})
@XmlSeeAlso({
    ExceptionType.class,
    AuthenticationExceptionType.class,
    AccessDeniedExceptionType.class,
    IllegalArgumentExceptionType.class
})
public class ExceptionInfoType {

    @XmlElement(name = "TransactionId")
    protected int transactionId;
    @XmlElement(name = "ErrorCode", required = true)
    protected BigInteger errorCode;
    @XmlElement(name = "Description", required = true)
    protected String description;
...
}

您知道为什么无法正确设置属性TransactionIdErrorCodeDescription吗?SOAP 故障字符串有什么问题?

标签: javasoapjaxbspring-ws

解决方案


我找到了答案,现在对象已正确呈现。命名空间必须是 100% 正确的。在我将 XSD 中的targetNamespace作为命名空间后,解组按预期工作。这意味着 SOAP 错误应该如下所示:

<SOAP-ENV:Fault xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <faultcode>SOAP-ENV:Server</faultcode>
  <faultstring xml:lang="en">fault</faultstring>
  <detail>
    <ExceptionInfoType xmlns="http://webservice-provider.de/BIG_V1.2">
        <ErrorCode>4</ErrorCode>
        <Description>The BIC is invalid</Description>
    </ExceptionInfoType>
  </detail>
</SOAP-ENV:Fault>

这就是 xmlns="http://webservice-provider.de/BIG_V1.2" 有所不同。


推荐阅读