Java – For SOAP, do we need to generate client stubs from WSDL

groovyjavasoapweb serviceswsdl

With SOAP, as with REST, am I safe in assuming that in the end all that the client does to call the web service is post some XML to an endpoint URL? If this is the case then is it required to use a tool like Axis or wsimport to generate the client stub classes from a WSDL?

Is there anything wrong with skipping this step and just building the raw XML myself and doing an HTTP Post of that entire SOAP envelope?

Here is what I want to do in groovy.

 def myXml = "<soapenv:Envelope>" +
            "  <soapenv:Body>" +
            "    <myservice:invoke>" +
            "        <username xsi:type=\"xsd:string\">$username</username>" +
            "        <apiKey xsi:type=\"xsd:string\">$apiKey</apiKey>" +
            "    </myservice:invoke>" +
            "  </soapenv:Body></soapenv:Envelope>"

 HttpMethod method = new PostMethod('https://myservice.javaguy.com/Service.asmx')

method.setRequestEntity(new StringRequestEntity(myXml))
method.setRequestHeader('Content-type','text/xml')
def responseCode = httpClient.executeMethod(method)

Best Answer

The statement "SOAP, as a REST" does not make any sense. REST is an architectural style. SOAP is a web services protocol that does not adhere to REST standards. A SOAP service can be invoked over HTTP.

If using SOAP over HTTP then yes you can invoke a SOAP by posting the SOAP message to the correct resource. There is a reasonable example of doing this on the SOAP wikipedia page linked to below. https://en.wikipedia.org/wiki/SOAP#Example_message

There is nothing wrong with avoiding the WSDL client generators, and building your own SOAP client. The generators are just there to do this for you to make it easier. The generated clients will handle managing the HTTP client, the deserialization/serialization logic, SOAP exception handling and translation for you. If you decide to not use them then you will have to write that logic yourself.

Related Topic