创建客户端处理SOAP消息和web服务的正常方式是;从 .xsd 模式生成 bean,并从 .wsdl 生成所有存根以调用 web 服务(在这种情况下,对于 Java,例如可以使用 JAXWS 和 JAXB)。
还请注意,通常.wsdl 定义服务,但如果您询问如何解析请求,最好显示.xsd。
当然,您当然可以调用 web 服务 使用直接和 apache http 客户端 左右进行 POST,然后处理响应...但请注意,这不推荐处理来自 SOAP Web 服务的大量请求和响应的方法,因为您必须手动解析每个响应以开展业务。假设这是您的情况,您可以执行类似的操作来处理您的 SOAP 消息(我使用 javax.xml.soap.SOAPMessage,因为您似乎想根据您在问题中输入的链接使用此类) .
例如,如果您收到如下 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 发生了一些事情。你能试试下面的代码吗
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);
}
}
希望对你有帮助,