有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JAVA中,如何从SOAP Web服务请求、将SOAP响应转换为XML并将其与另一个SOAP响应进行比较

我完全不熟悉这三个步骤,所以你能一步一步地帮我吗。(我懂Java语言,在这里和那里编写了两个脚本,但从未接触过SOAP内容)。 我需要这样做:

1)从两个SOAP服务请求并将响应存储在两个对象中

2)转换XML中的响应(可能,可能不,取决于输出是否为 <;标签></标签>; 那么不需要转换,但如果是 <;n32:标签>&书信电报;n32:标签>; 然后我会想摆脱“n32”

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

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

谢谢


共 (1) 个答案

  1. # 1 楼答案

    1. 由于您有webservice端点,我建议您为每个服务创建webservice客户端

    您可以使用JDK附带的wsimport来实现这一点:

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

    执行此命令后,您将有java对象来访问Web服务

    将这些对象放在项目中并访问webservice

    以下是如何操作的参考:

    JAXWS

    1. 既然已经有了响应的对象,就可以对每个属性进行编码和比较了。 如果需要再次将这些对象转换为xml进行比较(我再说一遍,因为SOAP消息已经是xml),可以使用xstreamhttp://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?