简体   繁体   中英

Does a SOAP client really need to use a WSDL?

I have seen examples of SOAP clients which just sent an XML payload of a SOAP envelope as an HTTP request body like so....

 String payload="""
    <soapenv:Envelope>
       <soapenv:Header/>
          <soapenv:Body>
               Some soap crapola
    </soapenv:Body>
  </soapenv:Envelope>
   """

    HttpPost httpPost = new HttpPost("somecraapysoapwebsevice.com/biteme");

    StringEntity entity = new StringEntity(payload);
    httpPost.setEntity(entity);
    httpPost.setHeader("Accept", "text/xml");
    httpPost.setHeader("Content-type", "text/xml");

    HttpResponse response = httpclient.execute(httpPost);

This code doesn't bother the WSDL. Could there be any problems with this approach?

Also if I were to use a WSDL to generate Java classes and send the request that way, would a server perceive the incoming as any different than what I did above?

There is no inherent issue creating SOAP requests like this, but there are some pitfalls:

  1. If the schema/wsdl of the service you're invoking changes, you'll have to reflect that in the code that creates the SOAP requests. If you use WSDL you just need to re-run your data-binder using the update WSDL/schema.
  2. Creating and parsing XML using strings isn't a great idea. You should use DOM, and serialise that, before sending it in your HTTP/SOAP request.

The SOAP service won't see any difference between clients using WSDL/data-binding and code that creates the SOAP requests in other ways. As long as your request matches the request schema, and you set your SOAP-Action HTTP header you should be fine.

In some cases there is a good argument for not using WSDL, where your component is a message broker (such as middleware) that needs to receive and forward loads of different message types, some of which you may not even have a WSDL definition for.

SOAP is mainly used in enterprise environments with hundreds of SOAP services and automated client stub generating is a necessity.

It is simpler to generate client stub from a given WSDL with all DTO objects considering all restrictions like nullable/non-nullable fields, complex fields, collections or so.

How would you manually build envelope for CreateContract request, when the contract may contain tens of fields of different types and many of them are collections or complex types with their own fields? The same with returned responses GetContract when you need to transform the body to a POJO

If you just need to call a single method with a single parameter you may find simpler to hardcode soap envelope with a placeholder for the parameter. It's up to you.

WSDL is just a contract between client and server. For the server, it doesn't matter how the request body is composed, by hand or with autogenerated code. If request complies to the contract server should accept it.

You can always check your hand made request for consistency with WSDL and test it using tools like SoapUI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM