繁体   English   中英

创建 QName 时局部部分不能为“null”

[英]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)

createElementNSsetAttrbuteNS以及第二个参数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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM