Showing posts with label SOAP Message. Show all posts
Showing posts with label SOAP Message. Show all posts

Thursday, December 19, 2013

Invoke SOAP Webservice dari java class

Berikut ini adalah contoh memanggil SOAP webservice dari sebuah class java tanpa menggunakan webservice proxy.

kita akan menggunakan wsdl : http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl sebagai contohnya.

kita akan memanggil method operation "LatLonListZipCode"

kemudian kita definisikan dulu SOAP message nya.

 <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:ndf="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">  
   <soapenv:Header/>  
   <soapenv:Body>  
    <ndf:LatLonListZipCode soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">  
      <zipCodeList xsi:type="xsd:string">19110</zipCodeList>  
    </ndf:LatLonListZipCode>  
   </soapenv:Body>  
 </soapenv:Envelope>  

Selanjutnya kita buat sample java class sebagai client nya.

 import java.io.ByteArrayInputStream;  
 import java.io.ByteArrayOutputStream;  
 import javax.xml.namespace.QName;  
 import javax.xml.transform.Source;  
 import javax.xml.transform.Transformer;  
 import javax.xml.transform.TransformerConfigurationException;  
 import javax.xml.transform.TransformerException;  
 import javax.xml.transform.TransformerFactory;  
 import javax.xml.transform.TransformerFactoryConfigurationError;  
 import javax.xml.transform.stream.StreamResult;  
 import javax.xml.transform.stream.StreamSource;  
 import javax.xml.ws.Dispatch;  
 import javax.xml.ws.Service;  
 import javax.xml.ws.soap.SOAPBinding;  
 import javax.xml.ws.soap.SOAPFaultException;  
 public class JAXWSClient {  
   final String WSDL_ENDPOINT_URL = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl";  
   String targetNS = "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl";  
   QName operationName = new QName(targetNS, "ndfdXML");  
   QName portQName = new QName(targetNS, "ndfdXMLPort");  
   public String invokeWebService(String inputMsg){  
     String responseStr=null;  
     try {  
       /**  
        * Define the service.  
        */  
       Service svc = Service.create(operationName);  
       svc.addPort(portQName,SOAPBinding.SOAP11HTTP_BINDING,WSDL_ENDPOINT_URL);  
       /**  
        * Create the dynamic invocation object from this service.  
        */  
       Dispatch<Source> dispatch = svc.createDispatch(portQName,Source.class,Service.Mode.MESSAGE);  
       ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputMsg.getBytes());  
       Source input = new StreamSource(byteArrayInputStream);  
       /**  
        * call WebService  
        */  
       Source response = dispatch.invoke(input);  
       /**  
       * get the  webservice response.  
       */  
       StreamResult result = new StreamResult(new ByteArrayOutputStream());  
       Transformer trans = TransformerFactory.newInstance().newTransformer();  
       trans.transform(response, result);  
       ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();  
       /**  
       * Write out the response content to string.  
       */  
       responseStr = new String(baos.toByteArray());  
       System.out.println("XML Response : "+responseStr);   
     }catch (SOAPFaultException e) {  
       System.out.println("SOAPFaultException: " + e.getFault().getFaultString());  
     } catch (TransformerConfigurationException e) {  
       e.printStackTrace();  
     } catch (TransformerFactoryConfigurationError e) {  
       e.printStackTrace();  
     } catch (TransformerException e) {  
       e.printStackTrace();  
     }   
     return responseStr;  
   }  
   public static void main(String[] args) {  
     try {     
       String xmlRequest = " <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:ndf=\"http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl\"> " +   
       "  <soapenv:Header/> " +   
       "  <soapenv:Body> " +   
       "   <ndf:LatLonListZipCode soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> " +   
       "     <zipCodeList xsi:type=\"xsd:string\">19110</zipCodeList> " +   
       "   </ndf:LatLonListZipCode> " +   
       "  </soapenv:Body> " +   
       "</soapenv:Envelope> ";  
      JAXWSClient jax_ws_client=new JAXWSClient();  
      String response=jax_ws_client.invokeWebService(xmlRequest);  
      //System.out.println("----- Response ----->:"+response);  
     }  
     catch (Exception t) {  
       t.printStackTrace();  
     }  
   }  
 }  

Outputnya menjadi :

 XML Response :   
 <?xml version = '1.0' encoding = 'UTF-8'?>  
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">  
 <SOAP-ENV:Header/>  
 <SOAP-ENV:Body>  
 <ns1:LatLonListZipCodeResponse xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">  
 <listLatLonOut xsi:type="xsd:string">&lt;?xml version='1.0'?>&lt;dwml version='1.0' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd'>&lt;latLonList>39.9525,-75.1657&lt;/latLonList>&lt;/dwml></listLatLonOut>  
 </ns1:LatLonListZipCodeResponse>  
 </SOAP-ENV:Body>  
 </SOAP-ENV:Envelope>  

Sekarang kita coba dari SOAP-UI



Hasilnya sama kan.

setelah kita dapatkan XML response nya, kita tinggal mengolah atau memparsing sesuai dengan kebutuhan.

selamat mencoba. :)