Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOAP request to WebService with java

I'm a bit confused about how to make a request to a webservice via java.

For now the only thing that I've understand is that webservices uses xml structured messages, but still I didn't quite understood how to structure my request.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <getProductDetails xmlns="http://magazzino.example.com/ws">       <productId>827635</productId>     </getProductDetails>   </soap:Body> </soap:Envelope> 

Basically I've to send 2 parameters to the web service and in return I expect two other parameters.

I guess there are some jars that can do most of the job, but I didn't find any online. Can someone please explain me the basis?

like image 307
Pievis Avatar asked Oct 10 '13 08:10

Pievis


People also ask

How do I create a SOAP request in Java?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

What is SOAP web service in Java?

SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing web services. SOAP is a W3C recommendation for communication between two applications.


1 Answers

A SOAP request is an XML file consisting of the parameters you are sending to the server.

The SOAP response is equally an XML file, but now with everything the service wants to give you.

Basically the WSDL is a XML file that explains the structure of those two XML.


To implement simple SOAP clients in Java, you can use the SAAJ framework (it is shipped with JSE 1.6 and above):

SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.

See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.

import javax.xml.soap.*;  public class SOAPClientSAAJ {      // SAAJ - SOAP Client Testing     public static void main(String args[]) {         /*             The example below requests from the Web Service at:              http://www.webservicex.net/uszip.asmx?op=GetInfoByCity               To call other WS, change the parameters below, which are:              - the SOAP Endpoint URL (that is, where the service is responding from)              - the SOAP Action              Also change the contents of the method createSoapEnvelope() in this class. It constructs              the inner part of the SOAP envelope that is actually sent.          */         String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx";         String soapAction = "http://www.webserviceX.NET/GetInfoByCity";          callSoapWebService(soapEndpointUrl, soapAction);     }      private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {         SOAPPart soapPart = soapMessage.getSOAPPart();          String myNamespace = "myNamespace";         String myNamespaceURI = "http://www.webserviceX.NET";          // SOAP Envelope         SOAPEnvelope envelope = soapPart.getEnvelope();         envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);              /*             Constructed SOAP Request Message:             <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="http://www.webserviceX.NET">                 <SOAP-ENV:Header/>                 <SOAP-ENV:Body>                     <myNamespace:GetInfoByCity>                         <myNamespace:USCity>New York</myNamespace:USCity>                     </myNamespace:GetInfoByCity>                 </SOAP-ENV:Body>             </SOAP-ENV:Envelope>             */          // SOAP Body         SOAPBody soapBody = envelope.getBody();         SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace);         SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace);         soapBodyElem1.addTextNode("New York");     }      private static void callSoapWebService(String soapEndpointUrl, String soapAction) {         try {             // Create SOAP Connection             SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();             SOAPConnection soapConnection = soapConnectionFactory.createConnection();              // Send SOAP Message to SOAP Server             SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);              // Print the SOAP Response             System.out.println("Response SOAP Message:");             soapResponse.writeTo(System.out);             System.out.println();              soapConnection.close();         } catch (Exception e) {             System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");             e.printStackTrace();         }     }      private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {         MessageFactory messageFactory = MessageFactory.newInstance();         SOAPMessage soapMessage = messageFactory.createMessage();          createSoapEnvelope(soapMessage);          MimeHeaders headers = soapMessage.getMimeHeaders();         headers.addHeader("SOAPAction", soapAction);          soapMessage.saveChanges();          /* Print the request message, just for debugging purposes */         System.out.println("Request SOAP Message:");         soapMessage.writeTo(System.out);         System.out.println("\n");          return soapMessage;     }  } 
like image 144
acdcjunior Avatar answered Oct 13 '22 21:10

acdcjunior