[英]java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName
I'm trying to add following xml to my soap body after converting it to document在将其转换为文档后,我试图将以下 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/>
but I'm getting error但我收到错误
java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName
here is the code I'm using for converting xml to soap body这是我用于将 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
following is the code which is converting xml to document以下是将 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;
}
Is my xml invalid?我的xml无效吗? Or I'm missing any configs while adding it to document?或者我在将它添加到文档时缺少任何配置?
Take a look at this post, looks like you need to add an element to your header specifying the QName and localPart.看看这篇文章,看起来您需要在标题中添加一个元素,指定 QName 和 localPart。
SOAP call fails when deployed on websphere but works fine on tomcat SOAP 调用在 websphere 上部署时失败,但在 tomcat 上工作正常
After whole hours in searching, I just wanted to share the Answer in this thread helped me with a Talend code migration - involving SOAP messages - from java8 to java11.经过几个小时的搜索,我只想分享这个线程中的答案帮助我进行了 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()
);
This throws the Local part cannot be "null" when creating a QName exception if you don't put the .setNamespaceAware(true)
instruction如果您不放置.setNamespaceAware(true)
指令,则在创建 QName 异常时抛出 Local 部分不能为“null”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.