从 Java 中的 SOAP 消息中获取字符串

Get Strings from SOAP Message in Java

如何从 SOAP 消息中获取特定部分并获取它们的值?

例如,如果 .wsdl 消息是这样的:

<wsdl:message name="theRequest">
      <wsdl:part name="username" type="xsd:string"/>
      <wsdl:part name="password" type="xsd:string"/>
      <wsdl:part name="someMsg"  type="xsd:string"/>
</wsdl:message>

我想获取 someMsg 值并将其保存到字符串变量中。

我在看这个:,但不是很明白。如果有人可以提供解释或任何类型的指南,我们将不胜感激!

创建客户端来处理 SOAP 消息和 web 服务 的正常方法是;从 .xsd 模式生成 bean,从 .wsdl 生成所有存根以调用 web 服务(在本例中为 Java 例如可以使用 JAXWSJAXB).

另请注意,通常 .wsdl 定义服务,但如果您询问如何解析请求,最好显示 .xsd

当然你可以调用一个 web 服务 直接使用和 apache http 客户端 左右做一个 POST然后处理响应...但请注意,这不是处理来自 SOAP Web 服务的大量请求和响应的推荐方法,因为您必须手动解析每个响应才能开展业务。假设这是你的情况,你可以做类似的事情来处理你的 SOAP 消息(我使用 javax.xml.soap.SOAPMessage 因为你似乎想使用这个基于 class在您提出问题的链接上)。

例如,如果您收到如下 SOAP 消息:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
    <theRequest>
        <username>user</username>
        <password>password</password>
        <someMsg>sooomeMessage</someMsg>
      </theRequest>
   </soapenv:Body>
</soapenv:Envelope>

您可以这样做:

import java.io.FileInputStream;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SOAPMessageTest {

    public static void main(String[] args) throws Exception {

        // create message factory
        MessageFactory mf = MessageFactory.newInstance();
        // headers for a SOAP message
        MimeHeaders header = new MimeHeaders();     
        header.addHeader("Content-Type", "text/xml");

        // inputStream with your SOAP content... for the 
        // test I use a fileInputStream pointing to a file
        // which contains the request showed below
        FileInputStream fis = new FileInputStream("/path/yourSOAPReq.xml");

        // create the SOAPMessage
        SOAPMessage soapMessage = mf.createMessage(header,fis);
        // get the body
        SOAPBody soapBody = soapMessage.getSOAPBody();
        // find your node based on tag name
        NodeList nodes = soapBody.getElementsByTagName("someMsg");

        // check if the node exists and get the value
        String someMsgContent = null;
        Node node = nodes.item(0);
        someMsgContent = node != null ? node.getTextContent() : "";

        System.out.println(someMsgContent);
    }

}

根据评论进行编辑:

它在 Java 8 中也适用于我,现在我唯一的猜测是 FileInputStream 发生了一些事情。您能否尝试使用相同的代码,但从 String 而不是 File.

获取请求
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SOAPMessageTest {

    public static void main(String[] args) throws Exception {

        // create message factory
        MessageFactory mf = MessageFactory.newInstance();
        // headers for a SOAP message
        MimeHeaders header = new MimeHeaders();     
        header.addHeader("Content-Type", "text/xml");

        String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
         "<soapenv:Body>"+
           "<theRequest>"+
             "<username>user</username>"+
             "<password>password</password>"+
             "<someMsg>sooomeMessage</someMsg>"+
           "</theRequest>"+
          "</soapenv:Body>"+
        "</soapenv:Envelope>";
        InputStream is = new ByteArrayInputStream(request.getBytes());

        // create the SOAPMessage
        SOAPMessage soapMessage = mf.createMessage(header,is);
        // get the body
        SOAPBody soapBody = soapMessage.getSOAPBody();
        // find your node based on tag name
        NodeList nodes = soapBody.getElementsByTagName("someMsg");
        System.out.println(nodes.getClass().getName());
        // check if the node exists and get the value
        String someMsgContent = null;
        Node node = nodes.item(0);
        someMsgContent = node != null ? node.getTextContent() : "";

        System.out.println(someMsgContent);
    }
}

希望对您有所帮助,