Rectangle 27 0

java How to send a SOAP request using WebServiceTemplate?

try {
            SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(

            WebServiceTemplate webServiceTemplate = new WebServiceTemplate(
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();



            Response response = (Response) webServiceTemplate

            Response msg = (Response) response;
        } catch (Exception s) {

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

if i don't set unmarshaller i get 'java.lang.IllegalStateException: No unmarshaller registered. Check configuration of WebServiceTemplate.' You can pass this exception with 'webServiceTemplate.setUnmarshaller(marshaller);'

Rectangle 27 0

java How to send a SOAP request using WebServiceTemplate?

private class MyTask extends AsyncTask<Void, Void, String> {
    protected void onPreExecute() {

    protected String doInBackground(Void... params) {
        try {
            SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);


            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            HttpTransportSE httpTransportSE = new HttpTransportSE(URL);

  , envelope);
            SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
            Log.d("TAG", "doInBackground: "+soapPrimitive.toString());

            return soapObject.toString();

        } catch (Exception e) {

        return null;

    protected void onPostExecute(String aVoid) {
private final String NAMESPACE = "";
private final String URL = "";
private final String SOAP_ACTION = "";
private final String METHOD_NAME = "CelsiusToFahrenheit";

2 Now you require the name of the method this WSDL has four method each of the are int attribute s:element with the value is the name of the Method in this case four methods are FahrenheitToCelsius, FahrenheitToCelsiusResponse, CelsiusToFahrenheit, CelsiusToFahrenheitResponse

3 Now you have to fure out the SOAP Action which is NAMESPACE+METHOD but WSDL also gives information about that as well, look for the tag soap:operation and it's soapAction attribute havs the Soap action as it's value in this case which we want to call is

@Jack, if possible, Kindly provide WSDL url, I will update the answer so that you can check

But unlike Rest, in Soap if you have WSDL URL you can get all the information needed to call the Soap call

Here is an Example what you should be looking for

It seems it is not using WebServiceTemplate of Spring framework am I right? I am interested in learning about SOAP support of Spring framework

Open [WSDL][1] in browser and check for the things which matter to call a remote method on server.

Soap has a lot of restriction unlike REST, It follows some standards which have to be meet before you get Network call to work,

The problem is, I do not have WSDL.

are you using soap:soap:2.3.1 library to make the soap call

this code was written in Android so you can ignore some part of it but I still kept it in answer so someone from android background can put a good use to it

Rectangle 27 0

java How to send a SOAP request using WebServiceTemplate?

    String endpoint = ""
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate().
    Object response = webServiceTemplate.marshalSendAndReceive(soapMsg);
    // handle you are response as you are currently doing.
    // Loop changing the endpoint as you need.

To send the same message to multiple endpoints you only need to loop on the sending code and the request handler.

Rectangle 27 0

java How to send a SOAP request using WebServiceTemplate?

// call fly service:
    FlyRequest flyRequest = new FlyRequest();
    Object flyResponse = webServiceTemplate.marshalSendAndReceive("", flyRequest);

    // call purchase service:
    PurchaseRequest purchaseRequest = new PurchaseRequest();
    Object purchaseResponse = webServiceTemplate.marshalSendAndReceive("", purchaseRequest);
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());

    WebServiceTemplate webServiceTemplate = new WebServiceTemplate(messageFactory);
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

package models;
import javax.xml.bind.annotation.XmlRootElement;

public class FlyRequest implements Serializable {

    private boolean nearByDeparture;

    public FlyRequest() {}

    public boolean isNearByDeparture() {
        return nearByDeparture;

    public void setNearByDeparture(boolean nearByDeparture) {
        this.nearByDeparture = nearByDeparture;

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

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

I used the code but it returns following Caused by: javax.xml.bind.JAXBException: "" doesnt contain ObjectFactory.class or jaxb.index

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

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

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

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.