SOAPMessage 的字符串在 java 中不起作用

String to SOAPMessage not working in java

我在字符串中有以下消息,

"<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://DefaultNamespace">
   <soapenv:Header/>
   <soapenv:Body>
      <def:XXX soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">someEncryptedTextGoesHere</in0>
      </def:XXX>
   </soapenv:Body>
</soapenv:Envelope>"

我试图在 SOAPMessage 中转换它并访问它的主体以获得 EncryptedText,但由于某些原因 envelop 变为 null,我不明白 我试过,

1) 基本上我有 HttpEntity,实体的主体是一个 soaprequest

public String getEncryptedCodeFromSOAPRequest(HttpEntity<byte[]> requestEntity) throws SOAPException, IOException {
    InputStream is = new ByteArrayInputStream(requestEntity.getBody());
    log.info(requestEntity.getBody().toString());
    String encryptedCode = "";
    if (is.available() > 0) {
        //SOAPMessage request= MessageFactory.newInstance().createMessage(null, is);
        SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
        encryptedCode = new String(request.getSOAPBody().getFirstChild().getFirstChild().getNodeValue().getBytes(), StandardCharsets.UTF_8);
    }
    return encryptedCode;
}

2) 尝试使用不同种类的 SOAPConstants,例如,

SOAPMessage request = MessageFactory.newInstance(SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE).createMessage(null,is);

URI_NS_SOAP_1_1_ENVELOPE 使用这个因为 xmlns:soapenv 在 soap msg 中似乎是这样的不知道这个登录是否正确。

如果您对此有任何建议,请告诉我

尝试替换这个:

if (is.available() > 0) {
    //SOAPMessage request= MessageFactory.newInstance().createMessage(null, is);
    SOAPMessage request = MessageFactory.newInstance().createMessage(null,is);
    encryptedCode = new String(request.getSOAPBody().getFirstChild().getFirstChild().getNodeValue().getBytes(), StandardCharsets.UTF_8);
}

有了这个:

    if (is.available() > 0) {
        SOAPMessage message = MessageFactory.newInstance("SOAP 1.2 Protocol").createMessage(null, is);
        Document requestDocument = message.getSOAPBody().extractContentAsDocument();
        NodeList nodes = requestDocument.getChildNodes();
        //get parent node 1
        Node parentNode = nodes.item(0);
        //get child nodes, of parent node
        NodeList childNodes = parentNode.getChildNodes();
        //get first child node since theres only one in the example xml
        Node childNode = childNodes.item(0);
        //print type, value, etc
        System.out.println(childNode.getNodeType() + " = " + childNode.getNodeName() + "/" + childNode.getNodeValue());
        encryptedCode = childNode.getNodeValue();
    }