java - 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;
...
}
您知道为什么无法正确设置属性TransactionId、ErrorCode和Description吗?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" 有所不同。
推荐阅读
- vba - 在 ms 访问表单中快速输入日期
- android - Restmock 服务器总是返回 500
- python - 从多列替换“.0”
- jquery - highcharts 中的错误 17:Highcharts-more js 文件未加载
- php - DIvi Theme Builder 出现未知错误,请稍后再试
- css - 在 Firefox 59.0 中隐藏滚动条
- javascript - 无法使用 node.js 根据 MongoDB 中的特定字符串对值进行分组?
- c# - 使用 lambda 表达式
- c# - IIS Express 路由在 Visual Sudio 2019 中无法正常工作
- google-chrome-devtools - chrome的devtools-audio功能数据展示的意义