Hey there, I have been working with JWSDL to allow me to programatically work with WSDL files. I now want to create SOAP requests that can be sent to the server. How do I generate these requests from the JWSDL classes? any ideas?
thanks!
Hey there, I have been working with JWSDL to allow me to programatically work with WSDL files. I now want to create SOAP requests that can be sent to the server. How do I generate these requests from the JWSDL classes? any ideas?
thanks!
You can do it like this:
Here, i have created a sample web service which takes two parameters number1 and number2. And gives the response as number3 (= number1 + number2). Web service is already deployed on localhost:8080 (tomcat server)
Your answer starts from here.
i have created a sample java file... which passes two parameters to a web service in a SOAP request and gets the SOAP response from the web service. You can get the parameters(described in a code) like getCalculation ,m , localhost:8080, number1, number2 and url from the WSDL file.
Sample Code:
package SampleJavaWSDLDemo;
public class SampleJavaWSDLDemo {
public static void main(String[] args)
{
try {
//Create a SOAPMessage
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();
Name bodyName = soapFactory.createName("getCalculation", "m", "http://localhost:8080/");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
//Insert Content
Name name = envelope.createName("number1");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("10");
name = envelope.createName("number2");
symbol = bodyElement.addChildElement(name);
symbol.addTextNode("20");
System.out.println("\n Request: \n");
message.writeTo(System.out);
System.out.println();
// Create an endpint point which is either URL or String type
URL endpoint = new URL("http://localhost:8080/WebServiceName/OperationName");
//Send a SOAPMessage (request) and then wait for SOAPMessage (response)
SOAPMessage response = connection.call(message, endpoint);
// Get the response from the webservice.
SOAPBody soapBody = response.getSOAPBody();
System.out.println("\n Response: \n");
TransformerFactory transformerfactory = TransformerFactory.newInstance();
Transformer transformer = transformerfactory.newTransformer();
Source sourceContent = response.getSOAPPart().getContent();
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
String resp = response.getSOAPBody().getElementsByTagName("return").item(0).getFirstChild().getNodeValue();
System.out.println("Answer is: " + resp);
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Try to run this code. It can give you a whole soap request and response message.