Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a SOAP request using WebServiceTemplate?

I am trying to send a request to a SOAP webservice. I read this tutorial and prepared the following code. However, I am going to send different requests to multiple SOAP webservices, whereas the tutorial focused on one request. How can I send SOAP request using WebserviceTemplate?

WebServiceTemplate

    SoapMessage soapMsg = new SoapMessage();     soapMsg.setUsername("Requester");     soapMsg.setPassword("Pass");     soapMsg.setLanguageCode("EN");     Request request = new Request();     request.setDeparture("FDH");     request.setDestination("HAM");     Date date = new Date();     SimpleDateFormat frm2 = new SimpleDateFormat("yyyy-MM-dd");     request.setDepartureDate(frm2.parse(frm2.format(date)));     request.setNumADT(1);     request.setNumCHD(0);     request.setNumInf(0);     request.setCurrencyCode("EUR");     request.setWaitForResult(true);     request.setNearByDepartures(true);     request.setNearByDestinations(true);     request.setRronly(false);     request.setMetaSearch(false);     soapMsg.setRequest(request);     WebServiceTemplate webServiceTemplate = new WebServiceTemplate().  //how to create object and send request!     Object response = webServiceTemplate.marshalSendAndReceive(             "https://aaa5.elsyarres.net", soapMsg);     Response msg = (Response) response;     System.err.println("size of results of wogolo:"             + msg.getFlights().getFlight().size()); 
like image 282
Jack Avatar asked Dec 19 '15 06:12

Jack


2 Answers

You can use following code, you do not need to define anything in xml file.

  try {             SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(                     MessageFactory.newInstance());             messageFactory.afterPropertiesSet();              WebServiceTemplate webServiceTemplate = new WebServiceTemplate(                     messageFactory);             Jaxb2Marshaller marshaller = new Jaxb2Marshaller();              marshaller.setContextPath("PACKAGE");             marshaller.afterPropertiesSet();              webServiceTemplate.setMarshaller(marshaller);             webServiceTemplate.afterPropertiesSet();              Response response = (Response) webServiceTemplate                     .marshalSendAndReceive(                             "address",                             searchFlights);              Response msg = (Response) response;         } catch (Exception s) {             s.printStackTrace();         } 
like image 105
Daniel Newtown Avatar answered Sep 19 '22 15:09

Daniel Newtown


To send different SOAP requests to different SOAP services, you just need to make your WebServiceTemplate aware of all requests and responses it will have to process.

Create a Java class for each request and response like so:

package models; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable;  @XmlRootElement public class FlyRequest implements Serializable {      private boolean nearByDeparture;      public FlyRequest() {}      public boolean isNearByDeparture() {         return nearByDeparture;     }      public void setNearByDeparture(boolean nearByDeparture) {         this.nearByDeparture = nearByDeparture;     } } 

(The @XmlRootElement is because we use JAXB marshaller below; see Jaxb reference for more info).

The setup of the template is done for example like so:

    SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());     messageFactory.afterPropertiesSet();      WebServiceTemplate webServiceTemplate = new WebServiceTemplate(messageFactory);     Jaxb2Marshaller marshaller = new Jaxb2Marshaller();     marshaller.setContextPath("models");     marshaller.afterPropertiesSet();      webServiceTemplate.setMarshaller(marshaller);     webServiceTemplate.afterPropertiesSet(); 

"models" is the name of the package where the Request/Responses classes are, so that jaxb can find them.

Then you just instantiate the request of the class you want to perform the call, like so:

    // call fly service:     FlyRequest flyRequest = new FlyRequest();     flyRequest.setNearByDeparture(false);     Object flyResponse = webServiceTemplate.marshalSendAndReceive("https://example.net/fly", flyRequest);      // call purchase service:     PurchaseRequest purchaseRequest = new PurchaseRequest();     purchaseRequest.setPrice(100);     Object purchaseResponse = webServiceTemplate.marshalSendAndReceive("https://example.net/purchase", purchaseRequest); 

Similarly, you can cast the response objects into your JAXB classes defined above.

like image 39
smarquis Avatar answered Sep 16 '22 15:09

smarquis