srini
srini

Reputation: 49

How to generate soap request xml from wsdl file in java

I am looking for some java opensource api for generating soap request xml file by passing wsdl_URL and operation name as parameters. Actually soapUI is doing this and I tried to go through the soapUI source code, but I am not able to understand the whole code to get my task done.

Is there any java api available to do this (apache or something)?

I spent couple of days in the net and didn't see any result.

If any body has any idea please help me.

Thanks in advance.

Upvotes: 4

Views: 30467

Answers (4)

Amir Azizkhani
Amir Azizkhani

Reputation: 1804

if you have SOAPHandler for request, you can print your xml like this:

public static String getRawXml(SOAPMessageContext context) {
        try {
            ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
            context.getMessage().writeTo(byteOS);
            return byteOS.toString("UTF-8");
        } catch (SOAPException | IOException e) {
            throw new RuntimeException(e);
        }
}

and call this method in handleMessage and handleFault.

in another way if you don't use apache or another library for calling soap service, use can see MessageWrapper class constructor in jdk manually and add break point on packet variable and see p.toString() in debug mode :)

MessageWrapper(Packet p, Message m) {
        super(m.getSOAPVersion());
        this.packet = p;
        this.delegate = m;
        this.streamDelegate = m instanceof StreamMessage ? (StreamMessage)m : null;
        this.setMessageMedadata(p);
    }

Upvotes: 0

George Vakras
George Vakras

Reputation: 93

You can use the open-source Membrane SOA library ([http://www.membrane-soa.org/soa-model-doc/1.4/java-api/create-soap-request-template.htm ]) to generate XMLs for each operation defined in the WSDL:

public void createTemplates(String url){

    WSDLParser parser = new WSDLParser();
    Definitions wsdl = parser.parse(url);   
    StringWriter writer = new StringWriter();
    SOARequestCreator creator = new SOARequestCreator();
    creator.setBuilder(new MarkupBuilder(writer));
    creator.setDefinitions(wsdl);

    for (Service service : wsdl.getServices()) {
        for (Port port : service.getPorts()) {
            Binding binding = port.getBinding();
            PortType portType = binding.getPortType();
            for (Operation op : portType.getOperations()) {
                creator.setCreator(new RequestTemplateCreator());
                creator.createRequest(port.getName(), op.getName(), binding.getName());
                System.out.println(writer);
                writer.getBuffer().setLength(0);
        }
    }
}

Upvotes: 5

Amit Bhati
Amit Bhati

Reputation: 5649

Soap UI also provide Java Api for creating request and response xml from WSDL.

 public static void main(String[] args) throws Exception {
        WsdlProject project = new WsdlProject();
        WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "http://localhost:8080/Service?wsdl");
        WsdlInterface wsdl = wsdls[0];
        for (Operation operation : wsdl.getOperationList()) {
            WsdlOperation wsdlOperation = (WsdlOperation) operation;
            System.out.println("Request:\n"+wsdlOperation.createRequest(true));
            System.out.println("\nResponse:\n"+wsdlOperation.createResponse(true));

        }
    }

Developer's corner of Soap UI has nice pointers for integrating with soap UI Api.

Upvotes: 3

Ben
Ben

Reputation: 13615

Have a look at AXIS

http://axis.apache.org/axis2/java/core/

Upvotes: 2

Related Questions