In this post, I will show you how to create java client for WSDL web services.
I will Create a example of currency converter, This is wsdl URL for same. “currency converter”
Request of “ConversionRate” method:
I will Create a example of currency converter, This is wsdl URL for same. “currency converter”
Request of “ConversionRate” method:
Response of “ConversionRate”
method:
Required jar files:
“junit-*.*.*.jar” and “javax-xml-soap-*.*.*.jar”
jar file add this to your project class path.
Below are the java Junit client java file for “ConversionRate”
method.
Approach 1:
package com.test; import java.io.StringWriter; import javax.xml.namespace.QName; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPConnection; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPBody; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamResult; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class SoapRequest1 { // reference variables private SOAPConnectionFactory soapConnectionFactory; private SOAPConnection soapConnection ; private MessageFactory messageFactory; private SOAPMessage soapMessage; private SOAPPart soapPart; private SOAPEnvelope soapEnvelope; @Before public void setUp(){ try { //object initialization soapConnectionFactory = SOAPConnectionFactory.newInstance(); soapConnection = soapConnectionFactory.createConnection(); messageFactory = MessageFactory.newInstance(); soapMessage = messageFactory.createMessage(); soapPart = soapMessage.getSOAPPart(); soapEnvelope = soapPart.getEnvelope(); } catch (UnsupportedOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SOAPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testGetCityForecastByZIP() { try { soapEnvelope.addNamespaceDeclaration("web", "http://www.webserviceX.NET/"); // change header's attribute MimeHeaders mimeHeader = soapMessage.getMimeHeaders(); mimeHeader.setHeader("SOAPAction", "http://www.webserviceX.NET/ConversionRate"); SOAPBody soapBody = soapEnvelope.getBody(); QName bodyName = new QName("http://www.webserviceX.NET/", "ConversionRate", "web"); SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName); QName n = new QName("http://www.webserviceX.NET/", "FromCurrency","web"); QName n1 = new QName("http://www.webserviceX.NET/", “ToCurrency", "web"); bodyElement.addChildElement(n).addTextNode("USD"); bodyElement.addChildElement(n1).addTextNode("ALL"); // Save message soapMessage.saveChanges(); // View input System.out.println("Soap request:\n"); soapMessage.writeTo(System.out); // Sending request String endUrl = “http://www.webservicex.com/CurrencyConvertor.asmx"; SOAPMessage soapResponseMessage = soapConnection. call(soapMessage, endUrl); // View the output System.out.println("XML response\n"); // Create transformer TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Get reply content Source source = soapResponseMessage.getSOAPPart(). getContent(); // Set output transformation StringWriter writer = new StringWriter(); StreamResult sResult = new StreamResult(writer); transformer.transform(source, sResult); String result1 = writer.toString(); System.out.println(result1); Assert.assertTrue(result1.contains("103.935")); // Close connection soapConnection.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
Approach 2: Create a msg file and copy request data, save file in your project as I saved Data1.msg.
Below the java client code to send request of Data1.msg
package com.test; import javax.xml.soap.MimeHeaders; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPConnection; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import java.io.FileInputStream; import java.io.StringWriter; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamResult; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class SoapRequest { private SOAPConnectionFactory soapConnectionFactory; private SOAPConnection soapConnection ; private MessageFactory messageFactory; private SOAPMessage soapMessage; private SOAPPart soapPart; @Before public void setUp(){ try { //creating connection object soapConnectionFactory = SOAPConnectionFactory.newInstance(); soapConnection = soapConnectionFactory.createConnection(); //creating message object messageFactory = MessageFactory.newInstance(); soapMessage = messageFactory.createMessage(); soapPart = soapMessage.getSOAPPart(); } catch (UnsupportedOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SOAPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testSOAPRequest() { try { // Object for message parts StreamSource prepMsg = new StreamSource(new FileInputStream("Data1.msg")); soapPart.setContent(prepMsg); // Save message soapMessage.saveChanges(); //change header's attribute MimeHeaders mimeHeader = soapMessage.getMimeHeaders(); mimeHeader.setHeader("SOAPAction", "http://www.webserviceX.NET/ConversionRate"); // Send request to url String endPoinUrl = "http://www.webservicex.com/CurrencyConvertor.asmx"; SOAPMessage messagerequest = soapConnection.call(soapMessage, endPoinUrl); // response data Source source = messagerequest.getSOAPPart().getContent(); // Create transformer object TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // set response parameter to string StringWriter writer = new StringWriter(); StreamResult sResult = new StreamResult(writer); transformer.transform(source, sResult); String result1 = writer.toString(); Assert.assertTrue(result1.contains("103.935")); System.out.println(result1); // close connection soapConnection.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
Very helpful document.
ReplyDelete