java - 使用 Java HttpServlet 解码 Soap 信封

标签 java soap axis

我正在尝试使用一个简单的 HttpServlet 来管理来自另一台服务器的 SOAP 请求。 该请求只有一个byte[]类型的参数(一个简单的字符串)。

相关代码是:

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
            InputStream is = req.getInputStream();
            byte[] body = IOUtils.toByteArray(is);
            String stringRequest = new String(body);
            log.info("Request -> "+stringRequest );
         }catch(Exception){log.error(e);}

我收到请求,如果我打印它,它会以这种方式显示:

    <?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <fixedResearch soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <MYPARAMETER xsi:type="xsd:hexBinary">
                *****bytearray******
            </MYPARAMETER>
        </fixedResearch>
    </soapenv:Body>
</soapenv:Envelope>

我需要获取 MYPARAMETER 标记内的值(它是一个 byte[])。 有一种聪明的方法,也许使用 Axis1 的一些 utils 类(我不能使用 Axis2)来净化传入的请求?

最佳答案

我不确定您可以使用什么,但“手动”方式是使用 XPath。下面的代码我运行了一次,它似乎有效。它没有利用命名空间,但它是一个开始。您需要优化它 - 这只是一个示例,但我已经有代码可以完成 99% 的工作。

package tld.domainname.stuff;


import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class XPathTest {
    public static void main(String[] argv) {
        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "    <soapenv:Body>\n" +
                "        <fixedResearch soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                "            <MYPARAMETER xsi:type=\"xsd:hexBinary\">\n" +
                "                *****bytearray******\n" +
                "            </MYPARAMETER>\n" +
                "        </fixedResearch>\n" +
                "    </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        String value = null;

        XPath xpath = XPathFactory.newInstance().newXPath();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        InputSource is = new InputSource(new StringReader(xmlString));
        try {
            doc = builder.parse(is);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (doc == null) {
            System.out.println("can't parse doc");
            return;
        }

        Node parentNode = doc.getDocumentElement();

        String path = "Body/fixedResearch/MYPARAMETER";

        NodeList nodeList;
        try {
            nodeList = (NodeList) xpath.evaluate(path, parentNode, XPathConstants.NODESET);
        } catch (XPathExpressionException xpe) {
            throw new IllegalArgumentException("Cannot evaluate xpath with path \"" + path + "\"", xpe);
        }

        if ((nodeList == null) || (nodeList.getLength() == 0)) {
            System.out.println("found nothing");
            return;
        }

        if (nodeList.getLength() > 1)
            System.out.println("found " + nodeList.getLength() + " nodes in the path \"" + path + "\" - using only the first");

        Node nextNode = nodeList.item(0);

        if (nextNode == null) {
            System.out.println("found nothing");
            return;
        }

        if (nextNode.hasChildNodes()) {
            Node child = nextNode.getFirstChild();
            value = child.getNodeValue();
        }

        System.out.println("found value of \"" + value + "\"");
    }
}

关于java - 使用 Java HttpServlet 解码 Soap 信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282671/

相关文章:

node.js - node-soap 更改操作的命名空间

java - Axis Web 服务日期时间转 java 日历

android - AChartEngine - Axis 的线宽

java - 无法连接到数据库

java - 将整数数组编码为图像的策略

java - 扫描仪上的数字类型

soap - JAX-WS xsi :type attributes missing

java - 抛出主要异常和子类型,有没有正确的方法?

java - 使用 java 指定 SOAP 响应中元素的顺序

java - 不同格式的 Axis SOAP 响应是否相同?