繁体   English   中英

java.lang.IllegalArgumentException:创建 QName 时本地部分不能为“null”

[英]java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName

在将其转换为文档后,我试图将以下 xml 添加到我的 soap 正文中

<ns2:OSSRequest xmlns:ns2="http://www.someurl">
<requestBody>
    <property>
        <address>
            <addressId>someValue</addressId>
            <municipality>someValue</municipality>
            <postalCode>someValue</postalCode>
            <province>someValue</province>
            <streetNumber>someValue</streetNumber>
        </address>
    </property>
    <requestedProducts>
        <products>someValue</products>
        <products>someValue</products>
    </requestedProducts>
</requestBody>
<requestHeader/>

但我收到错误

java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName

这是我用于将 xml 转换为 soap 正文的代码

MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header_soap = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
Document document = convertStringToDocument(xml);
body.addDocument(document); // getting error on this line

以下是将 xml 转换为文档的代码

private Document convertStringToDocument(String xmlStr) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

我的xml无效吗? 或者我在将它添加到文档时缺少任何配置?

看看这篇文章,看起来您需要在标题中添加一个元素,指定 QName 和 localPart。

SOAP 调用在 websphere 上部署时失败,但在 tomcat 上工作正常

经过几个小时的搜索,我只想分享这个线程中的答案帮助我进行了 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”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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