【问题标题】:How to request from a SOAP webservice, convert the SOAP response to XML and compare it to another SOAP response, in JAVA如何从 SOAP Web 服务请求,将 SOAP 响应转换为 XML 并将其与另一个 SOAP 响应进行比较,在 JAVA 中
【发布时间】:2014-11-10 13:57:42
【问题描述】:

我在执行这 3 个步骤方面是全新的,所以请您逐步帮助我。 (我懂 Java 语言,在这里和那里做过几个脚本,但从未接触过 SOAP 的东西)。 我需要这样做:

1) 来自两个 SOAP 服务的请求并将响应存储在两个对象中。

2) 在 XML 中转换响应(可能,也可能不是,取决于输出是否采用格式 标记> 那么不需要转换,但如果是 那么我会想摆脱“n32”。

3) 比较这两个响应,看看节点/标签的差异在哪里,可能在标签级别(可能使用 XMLUnit)

4) 在控制台中报告差异。(在 JUnit 中不是错误)。

谢谢!

【问题讨论】:

    标签: java xml web-services soap automation


    【解决方案1】:
    1. 由于您有 webservice 端点,我建议您为每个服务创建 webservice 客户端。

    您可以使用 JDK 自带的 wsimport

    wsimport.bat -d "D:\WS" -keep -verbose endpoint_ws.wsdl
    pause
    

    执行此命令后,您将拥有访问 web 服务的 java 对象。

    将这些对象放入您的项目中并访问网络服务。

    这里是如何做的参考:

    JAXWS

    1. 现在您有了响应对象,您可以对每个属性进行编码和比较。 如果需要再次将这些对象转换为 xml 进行比较(我再说一遍,因为 SOAP 消息已经是 xml),您可以使用 xstream (http://x-stream.github.io/tutorial.html)。

    已编辑

    如果你不需要处理 java 客户端对象,你可以关注这个帖子:

    How to do a SOAP Web Service call from Java class?

    在文章的第二部分,展示了如何直接与请求/响应消息进行交互:

    import javax.xml.soap.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.*;
    
    public class SOAPClientSAAJ {
    
        /**
         * Starting point for the SAAJ - SOAP Client Testing
         */
        public static void main(String args[]) {
            try {
                // Create SOAP Connection
                SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
                SOAPConnection soapConnection = soapConnectionFactory.createConnection();
    
                // Send SOAP Message to SOAP Server
                String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
                SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
    
                // Process the SOAP Response
                printSOAPResponse(soapResponse);
    
                soapConnection.close();
            } catch (Exception e) {
                System.err.println("Error occurred while sending SOAP Request to Server");
                e.printStackTrace();
            }
        }
    
        private static SOAPMessage createSOAPRequest() throws Exception {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
    
            String serverURI = "http://ws.cdyne.com/";
    
            // SOAP Envelope
            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration("example", serverURI);
    
            /*
            Constructed SOAP Request Message:
            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
                <SOAP-ENV:Header/>
                <SOAP-ENV:Body>
                    <example:VerifyEmail>
                        <example:email>mutantninja@gmail.com</example:email>
                        <example:LicenseKey>123</example:LicenseKey>
                    </example:VerifyEmail>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
             */
    
            // SOAP Body
            SOAPBody soapBody = envelope.getBody();
            SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
            SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
            soapBodyElem1.addTextNode("mutantninja@gmail.com");
            SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
            soapBodyElem2.addTextNode("123");
    
            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");
    
            soapMessage.saveChanges();
    
            /* Print the request message */
            System.out.print("Request SOAP Message = ");
            soapMessage.writeTo(System.out);
            System.out.println();
    
            return soapMessage;
        }
    
        /**
         * Method used to print the SOAP Response
         */
        private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            Source sourceContent = soapResponse.getSOAPPart().getContent();
            System.out.print("\nResponse SOAP Message = ");
            StreamResult result = new StreamResult(System.out);
            transformer.transform(sourceContent, result);
        }
    
    }
    

    已编辑

    要直接从字符串创建soap消息,首先创建一个InputStream

    InputStream is = new ByteArrayInputStream(send.getBytes());
    SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);
    

    更多信息:

    How to convert a string to a SOAPMessage in Java?

    【讨论】:

    • 没有更简单的方法吗?我只需要将请求发送到 WS 端点并获得响应。
    • 是的,它有帮助,谢谢。我可以在哪里放置我的请求消息(多行)?
    • 在给出的示例中,请求消息是在 createSOAPRequest() 方法中构造的。
    • 查看添加的新信息是否有帮助。
    • 谢谢,但我最终使用了它并且它有效,但它很奇怪,有时我得到响应并且80%我不会得到它:tinyurl.com/oa3gurc
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    相关资源
    最近更新 更多