![](/img/trans.png)
[英]Opensaml unmarshallMessage giving opensaml local part cannot be "null" when creating a QName Exception
[英]Local part cannot be "null" when creating a QName
我們正試圖追蹤一個錯誤。 我們在日志中收到上述錯誤。
任何人都可以解釋此消息的含義嗎? 收到此消息有任何典型原因嗎?
堆棧跟蹤是:
org.apache.axiom.om.OMException: java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:206)
at org.apache.axiom.om.impl.llom.OMNodeImpl.build(OMNodeImpl.java:318)
at org.apache.axiom.om.impl.llom.OMElementImpl.build(OMElementImpl.java:618)
at org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.toOM(SAAJConverterImpl.java:147)
at org.apache.axis2.jaxws.message.impl.XMLPartImpl._convertSE2OM(XMLPartImpl.java:77)
at org.apache.axis2.jaxws.message.impl.XMLPartBase.getContentAsOMElement(XMLPartBase.java:203)
at org.apache.axis2.jaxws.message.impl.XMLPartBase.getAsOMElement(XMLPartBase.java:255)
at org.apache.axis2.jaxws.message.impl.MessageImpl.getAsOMElement(MessageImpl.java:464)
at org.apache.axis2.jaxws.message.util.MessageUtils.putMessageOnMessageContext(MessageUtils.java:202)
at org.apache.axis2.jaxws.core.controller.AxisInvocationController.prepareRequest(AxisInvocationController.java:370)
at org.apache.axis2.jaxws.core.controller.InvocationController.invoke(InvocationController.java:120)
at org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler.invokeSEIMethod(JAXWSProxyHandler.java:317)
at org.apache.axis2.jaxws.client.proxy.JAXWSProxyHandler.invoke(JAXWSProxyHandler.java:148)
嘗試從String構造org.w3c.dom.Document時,出現了相同的錯誤消息(創建QName時本地部分不能為“ null”)。 在DocumentBuilderFactory上調用setNamespaceAware(true)后,問題消失了。 工作代碼段如下所示。
private static Document getDocumentFromString(final String xmlContent)
throws Exception
{
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
try
{
return documentBuilderFactory
.newDocumentBuilder()
.parse(new InputSource(new StringReader(xmlContent)));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
這意味着您正在使用諸如createElementNS
之類的名稱空間方法之一來創建DOM元素或屬性,因此
document.createElementNS(namespace, null)
或createElementNS
或setAttrbuteNS
以及第二個參數qname為null
,或包含前綴但不包含本地部分,如"foo:"
。
編輯:
我會嘗試運行通過驗證器解析的XML。 可能有一些標記或屬性名稱,例如foo:
或foo:bar:baz
,它是有效的XML標識符,但根據XML名稱空間引入的其他限制,它們是無效的。
經過幾個小時的搜索,我只想分享這個線程中的答案幫助我進行了 Talend 代碼遷移——涉及 SOAP 條消息——從 java8 到 java11。
// Used libraries
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.SOAPBody;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
...
// This is the node I want to replace: "<DataArea><Contact/></DataArea>"
// <SOAP-ENV:Body> > SOAP Action (e.g. <ns:GetContact>) > <GetContactRequest> > <DataArea>
SOAPBody soapBodyRequest = objSOAPMessage.getSOAPPart().getEnvelope().getBody();
Node nodeDataArea = soapBodyRequest.getFirstChild().getFirstChild().getFirstChild();
// Build a valid Node object starting from a string e.g. "<Contact> etc etc nested-etc </Contact>"
DocumentBuilderFactory objDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
// As per java11, this is essential. It forces the Factory to consider the ':' as a namespace separator rather than part of a tag.
objDocumentBuilderFactory.setNamespaceAware(true);
// Create the node to replace "<DataArea><Contact/></DataArea>" with "<DataArea><Contact>content and nested tags</Contact></DataArea>"
Node nodeParsedFromString = objDocumentBuilderFactory.newDocumentBuilder().parse(new ByteArrayInputStream(strDataArea.getBytes())).getDocumentElement();
// Import the newly parsed Node object in the request envelop being built and replace the existing node.
nodeDataArea.replaceChild(
/*newChild*/nodeDataArea.getOwnerDocument().importNode(nodeParsedFromString, true),
/*oldChild*/nodeDataArea.getFirstChild()
);
如果您不放置.setNamespaceAware(true)
指令,則在創建 QName 異常時拋出 Local 部分不能為“null”
盡管這是一個舊線程,但我希望這個答案可以幫助其他人搜索此錯誤。 當我嘗試使用maven-enunciate-cxf-plugin:1.28構建Web應用程序時,遇到了相同的錯誤。
對我來說,這是在我向Web服務簽名中添加了檢查的異常后引起的:
@WebMethod(operationName = "enquirySth")
public IBANResponse enquirySth(String input) throws I
InvalidInputException { ...
我已經使用JAX-WS Spec進行異常拋出,但是沒有成功。 最后,我在Enunciate問題跟蹤器系統中發現了此問題 ,這表明此問題已在當前版本中解決,但我認為它仍然存在。
最終,我完成了以下變通辦法來解決我的問題:將@XmlRootElement添加到我的FaultBean中。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FaultBean",propOrder = {"errorDescription", "errorCode"})
public class FaultBean implements Serializable {
@XmlElement(required = true, nillable = true)
protected String errorDescription;
@XmlElement(required = true, nillable = true)
protected String errorCode;
public FaultBean() {
}
public String getErrorDescription() {
return this.errorDescription;
}
public void setErrorDescription(String var1) {
this.errorDescription = var1;
}
public String getErrorCode() {
return this.errorCode;
}
public void setErrorCode(String var1) {
this.errorCode = var1;
}
}
而已。 希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.