簡體   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