21

I would like to ask you how I can generate a SOAP request/response in a XML format on the basis of the WSDL file. The target platform is JVM so a wide set of languages can be used (e.g. Java, Scala, Groovy, JRuby, Jython, etc.). The SOAP request/response generation should be done purely on the XML level without any class-generation and class-loading (WSDL2Java, JAXB or similar approaches are inappropriate in this case). Generation should be done programmatically with the usage of open-source components. The generation technique should support document-literal, rpc-encoded and rpc-literal flavors, so proper encoding of parameters should be handled by the generator. Request/response messages should be fully-populated -> empty nodes should be generated even for empty/blank values.

Cutting the long story short -> I would like to do programmatically the thing that is doable in SoapUI IDE. I already had a look at different Java-related libraries/frameworks (SAAJ, WSDL4J) or Ruby (Savon) but I am struggling to move it any further.

A sample Web-Service definition (WSDL and XSD) that I am working on is stockquote-ws.wsdl and stockquote-schema.xsd.

What I would like to do is:

SoapMessageGenerator generator = new SoapMessageGenerator("stockquote-ws.wsdl");
String request = generator.generateSoapRequest();
String response = generator.generateSoapResponse();

In this case a request should look like this:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://centeractive.com/stockquote.wsdl" xmlns:stoc1="http://centeractive.com/stockquote.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:GetLastTradePrice soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <stoc1:TradePriceRequest>
            <tickerSymbol xsi:type="xsd:string">?</tickerSymbol>
         </stoc1:TradePriceRequest>
      </stoc:GetLastTradePrice>
   </soapenv:Body>
</soapenv:Envelope>

... whereas a response should look like this:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://centeractive.com/stockquote.wsdl" xmlns:stoc1="http://centeractive.com/stockquote.xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:GetLastTradePriceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <stoc1:TradePrice>
            <price xsi:type="xsd:float">?</price>
         </stoc1:TradePrice>
      </stoc:GetLastTradePriceResponse>
   </soapenv:Body>
</soapenv:Envelope>
Cœur
  • 37,241
  • 25
  • 195
  • 267
tom.bujok
  • 1,612
  • 2
  • 13
  • 20
  • 2
    Why not take a look at how SoapUI does it? ([Link](http://sourceforge.net/projects/soapui/files/soapui/4.0.1/soapui-4.0.1-src.zip/download) to sourcecode) – Wivani Sep 20 '11 at 15:36
  • I had a look at the SoapUI source code and it is not the best way to go. Classes are tightly-coupled, user-interface and business logic code is intermingled - there's no separation of concerns whatsoever. – tom.bujok Sep 21 '11 at 07:19
  • 1
    The other thing is that the SoapUI code is incomplete. I have just wrote a simple extractor to extract the SoapMessageBuilder class and 188 classes are missing. Yeah, that is a fully open-source solution... – tom.bujok Sep 21 '11 at 09:35
  • I have found the following webapp very helpful in generate sample SOAP messages from the given WSDL http://www.soapclient.com/soapmsg.html – Hemant Nov 21 '12 at 06:57

5 Answers5

18

OK. I managed to fully solve this problem. I have extracted some code from soapUI and started an open-source project to support SOAP in a purely XML way in Java. The main reason behind the class extraction was to separate the code that is responsible for the generation of the SOAP messages from the rest of the soapUIs code that is tightly coupled with other modules, such as soapUIs graphical user interface, etc. You can find the project here: https://github.com/reficio/soap-ws Not only is it able to generate SOAP messages, but also provides SOAP client and server. More details here: http://www.reficio.org/projects

tom.bujok
  • 1,612
  • 2
  • 13
  • 20
  • I couldn't manage sending parameter in request especially for java not grovy. I couldn't find any clear example or something. – Ismail Sahin Sep 18 '14 at 15:47
  • There's a clear example in the sub project called (surprise, surprise) "soap-examples". There are Java and Groovy examples there: https://github.com/reficio/soap-ws/tree/master/soap-examples/quickstart/src/test – tom.bujok Sep 19 '14 at 17:40
  • is there any library for c# .net? – Reza Akraminejad Sep 23 '15 at 07:28
  • @tom.bujok When I downloaded the source code I get an error in my pom.xml for the execution tag. – JayC Jan 20 '17 at 14:21
5

What about the SOAPUI library:

package com.bbog.soap;

import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
import com.eviware.soapui.model.iface.Operation;

public class WsdlAnalyzer {

    public static void main(String[] args) throws Exception {
        WsdlProject project = new WsdlProject();
        WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "http://localhost:7000/Solicitud?wsdl");
        WsdlInterface wsdl = wsdls[0];
        for (Operation operation : wsdl.getOperationList()) {
            WsdlOperation op = (WsdlOperation) operation;
            System.out.println("OP:"+op.getName());
            System.out.println(op.createRequest(true));
            System.out.println("Response:");
            System.out.println(op.createResponse(true));
        }
    }
}
Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
sonnykwe
  • 59
  • 1
  • 2
  • 1
    Could also mention please what library (or libraries) are required for this? And possibly where they could be downloaded? thnx. – Larry Jul 02 '12 at 16:17
  • soapui-x.y.z.jar Can be downloaded from http://www.soapui.org/repository/eviware/jars/ – Cid Dec 07 '12 at 08:34
  • I am using this, but this code doesn't stop. And I don't find any close() methods... – Trick Apr 16 '14 at 12:11
  • This lib doesn't see to be available or it is way out of date right? – djangofan Feb 05 '21 at 19:51
0

I am actually looking to do the same thing. I've been using the javax.wsdl API to pull information from the wsdl and I'm trying to use javax.xml.soap API to create the SOAP request/response. They might be worth taking a look at.

  • 2
    Welcome to stackoverflow. This should probably be a comment rather than an answer (Though I realize you may not have sufficient points to post comments yet). – Leigh Jun 13 '12 at 20:41
0

You might be interested in kSOAP project which is used in mobile development. The following kSOAP tutorial will point you to how to serialize the request and the following section show you how to get the response.

kSOAP could create the SOAP message without having to generate the proxy code. This is needed in mobile development due to its processing power that is considerably less than the desktop and having proxy classes and library are considered heavier than directly creating the SOAP message

momo
  • 21,233
  • 8
  • 39
  • 38
  • Thanks. I had a look at kSOAP and it is not what I am looking for. In kSOAP you have to build the request body by hand (manually inserting fields). Furthermore, the project doesn't seem to be actively developed. – tom.bujok Sep 21 '11 at 07:57
0

IBM article

The above article seems to address the technique that I would try for your case: make use of XSLT transformation. After all you're going from XML to XML. If you have better luck than I finding (or developing of course) the specific XSLT stylesheet(s) that you need to go from a WSDL to accompanying SOAP request(s) I'd love to learn about it.

Cheers, Wim

Wivani
  • 2,036
  • 22
  • 28