4

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.

4 Answers 4

5

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

Have a look at AXIS

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

3 Comments

Hello Benjamin, thanks for your reply. I did not find any useful java api to create request xml file like in soapUI. They were talking about ServiceClient class but I dont understand how to use that one.
The general way how I work with soap and java, is to generate an object model from your wsdl and work against that. like so ws.apache.org/axis/java/…
0

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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.