Call SOAP service from java Spring boot

Aviva 26 Published: 09/11/2024

Call SOAP service from java Spring boot

I'll respond in English.

To call a SOAP (Simple Object Access Protocol) service from a Java-based application, you can use the org.springframework.ws module provided by Spring Boot. Here's an example of how to do it:

First, add the following dependencies to your pom.xml file (if you're using Maven):


org.springframework

spring-web-services

3.2.8.RELEASE

Next, create a Java class that will handle the SOAP request and response:

import org.springframework.ws.client.core.WebServiceTemplate;

import org.springframework.ws.soap.SOAPMessage;

public class SoapClient {

private final WebServiceTemplate webServiceTemplate;

public SoapClient() {

this.webServiceTemplate = new WebServiceTemplate();

}

public String callSoapService(String soapAction, String requestXml) throws Exception {

SOAPMessage soapMessage = createSoapMessage(soapAction, requestXml);

JAXBElement response = webServiceTemplate.marshalSendAndReceive(soapMessage);

SomeResponseClass responseObject = response.getValue();

return responseObject.toString();

}

private SOAPMessage createSoapMessage(String soapAction, String requestXml) throws Exception {

// Create a new SOAP message

SoapMessage soapMessage = new SoapMessage();

// Set the SOAP action

soapMessage.setSoapAction(soapAction);

// Add the request XML to the message body

soapMessage.getBody().addTextNode(requestXml);

return soapMessage;

}

}

In this example, we're creating a SoapClient class that has a method called callSoapService. This method takes three parameters: soapAction, which is the name of the SOAP action to call; requestXml, which is the XML request data; and returns the response from the SOAP service.

The createSoapMessage method creates a new SOAP message with the specified soap action and adds the request XML to the message body. The marshalSendAndReceive method is used to send the SOAP request to the server and receive the response.

To use this class, you'll need to create an instance of it and then call the callSoapService method:

SoapClient soapClient = new SoapClient();

String response = soapClient.callSoapService("someAction", "");

Make sure to replace "someAction" with the actual name of your SOAP action, and <requestXml> with the actual XML request data.

I hope this helps! Let me know if you have any further questions.

Java SOAP request example

Here's an example of a Java SOAP request using the Apache Axis2 library:

Prerequisites

You need to have Apache Axis2 installed on your system. You need to have a SOAP-based web service that accepts requests.

Java Code

import org.apache.axis2.AxisFault;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.client.TransportSendRequestExecutor;

// Set the endpoint URL of the SOAP web service

String endpointURL = "http://localhost:8080/soap/webService";

// Create a ServiceClient object to send the request

ServiceClient client = new ServiceClient();

client.setEndpoint(endpointURL);

// Set the SOAPAction header for the request

String soapAction = "http://example.com/mySOAPAction";

client.getOptions().setProperty(org.apache.axis2.client.Options.SOAP_ACTION, soapAction);

// Create a SoapEnvelope object to contain the SOAP request

SoapEnvelope envelope = new SoapEnvelope();

// Add the SOAP elements to the envelope

envelope.addBodyElement(new MySOAPElement());

// Set the transport executor for the request

TransportSendRequestExecutor executor = (TransportSendRequestExecutor) client.getTransport().getExecutor();

executor.setSendRequest(true);

// Send the SOAP request and get the response

SoapResponse response = (SoapResponse) client.send(envelope);

In this example, we create a ServiceClient object that connects to the SOAP web service at the specified endpoint URL. We set the SOAP_ACTION property on the Options object to specify the SOAP action for the request.

We then create a SoapEnvelope object that will contain the SOAP request. In this case, we add a MySOAPElement object as the body of the envelope (you would replace this with your own SOAP elements).

Next, we set the transport executor for the request to enable sending of the request. Finally, we send the SOAP request using the send() method and get the response as a SoapResponse object.

SOAP Request Example


<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/">

</soap:Envelope>

This is a simple example of a SOAP request that you could use as the basis for your own requests. The <mySOAPAction> element would contain the actual elements and data required by the web service.

Note

This is just an example code snippet and may not work in its current form. You will need to modify the code to suit your specific SOAP web service and requirements. Apache Axis2 has many more features and options for sending SOAP requests, so be sure to check out their documentation if you're using this library.