在java中创建和发送SOAP消息到一个非常简单的Web服务

时间:2015-06-11 09:18:32

标签: java web-services soap wsdl xml-namespaces

我目前正在尝试创建SOAP消息并将其发送到我自己创建的一个非常简单的Web服务。

显然web服务有一个wsdl文件,你可以在这里看到:

wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://service.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://service.com">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.com">
<xs:element name="echoTest">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="echoString" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="echoTestResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="echoTestRequest">
<wsdl:part name="parameters" element="ns:echoTest"/>
</wsdl:message>
<wsdl:message name="echoTestResponse">
<wsdl:part name="parameters" element="ns:echoTestResponse"/>
</wsdl:message>
<wsdl:portType name="RequestHandlerPortType">
<wsdl:operation name="echoTest">
<wsdl:input message="ns:echoTestRequest" wsaw:Action="urn:echoTest"/>
<wsdl:output message="ns:echoTestResponse" wsaw:Action="urn:echoTestResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="RequestHandlerSoap11Binding" type="ns:RequestHandlerPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="echoTest">
<soap:operation soapAction="urn:echoTest" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="RequestHandlerSoap12Binding" type="ns:RequestHandlerPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="echoTest">
<soap12:operation soapAction="urn:echoTest" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="RequestHandlerHttpBinding" type="ns:RequestHandlerPortType">
<http:binding verb="POST"/>
<wsdl:operation name="echoTest">
<http:operation location="echoTest"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="RequestHandler">
<wsdl:port name="RequestHandlerHttpSoap11Endpoint" binding="ns:RequestHandlerSoap11Binding">
<soap:address location="http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler.RequestHandlerHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="RequestHandlerHttpSoap12Endpoint" binding="ns:RequestHandlerSoap12Binding">
<soap12:address location="http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler.RequestHandlerHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="RequestHandlerHttpEndpoint" binding="ns:RequestHandlerHttpBinding">
<http:address location="http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler.RequestHandlerHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

然后我使用了名为Wizdler的谷歌浏览器插件来了解肥皂信息的结构应该是什么样子。这个例子的Soap消息应该是这样的。

<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope">
    <Body>
        <echoTest xmlns="http://service.com">
            <echoString>[string?]</echoString>
        </echoTest>
    </Body>
</Envelope>

这是一个非常简单和直接的SOAP消息..然而,我可以让它工作。 我正在尝试使用以下代码创建SOAP消息:

        MessageFactory messageFactory = MessageFactory.newInstance();

        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://service.com";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        soapBody.addNamespaceDeclaration("", serverURI);
        SOAPElement soapBodyElem = soapBody.addChildElement("echoTest");

        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("echoString");
        soapBodyElem1.addTextNode("HELLO?!?");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("RequestHandler", serverURI);

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message:");
        soapMessage.writeTo(System.out);
        System.out.println();

上面的代码产生以下SOAP消息:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/>
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body xmlns="http://service.com">
      <echoTest xmlns="">
          <echoString xmlns="http://service.com">HELLO?!?</echoString> 
      </echoTest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我不知道为什么echoString得到xmlns =“http://service.com”..它应该在echoTest标签中而不是......我猜这可能是问题的一部分。

响应消息如下所示:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body>
   <soapenv:Fault>
      <faultcode>soapenv:Server</faultcode>
      <faultstring>namespace mismatch require http://service.com found none</faultstring>
      <detail />
   </soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>

可以看出,命名空间存在问题,但我似乎无论做什么,问题仍然存在。

我希望有人能看到问题,也许可以提供解决方案。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

SOAPElement soapBodyElem = soapBody.addChildElement("echoTest","", serverURI);

SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("echoString","", serverURI);

但是为什么要显式构建SOAP?

如果没有特殊原因,请使用wsimport。 Java类+ JAXB注释将对您有所帮助。

答案 1 :(得分:0)

无论如何,在尝试了一段时间之后,我终于开始工作......当然,在发布这个问题之后,我就让它发挥作用了。所以这里是问题中描述的问题的完整Java代码:

public class PayCheckHandler {

    public PayCheckHandler() throws Exception {
        // TODO Auto-generated method stub

        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "http://localhost:8181/PayCheckHandlerMainWebService/services/RequestHandler?wsdl";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // print SOAP Response
        System.out.println();
        System.out.print("Response SOAP Message:");
        soapResponse.writeTo(System.out);
        soapConnection.close();

    }


    private static SOAPMessage createSOAPRequest() throws Exception {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();

            String serverURI = "http://service.com";

            // SOAP Envelope
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPHeader header = envelope.getHeader();
            SOAPBody body = envelope.getBody();


            // SOAP Body
            SOAPBodyElement element = body.addBodyElement(envelope.createName("echoTest", "", serverURI));
            element.addChildElement("echoString").addTextNode("Hello!!!");



            return soapMessage;
    }

    public static void main(String[] args) throws Exception {
        new PayCheckHandler();
    }

}

顺便说一下。如果我绝对不必手动创建自己的SOAP消息,我会像artem.ponchakov建议的那样。