无法解组 SOAP 错误

SOAP fault cannot be unmarshalled

我的问题是 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) 没有正确映射 values/attributes:

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

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

@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 错误字符串有什么问题?

我找到了答案,现在对象已正确呈现。名称空间必须 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" 与众不同。