How to hit soap request in java using wsdl

Jade 68 Published: 08/02/2024

How to hit soap request in java using wsdl

To consume a SOAP web service (WS) in Java, you need to follow these steps:

Generate the Stub Code: Use a tool like Apache CXF or JAX-WS (Java API for XML Web Services) to generate the stub code for your WS client. This step involves creating a Java class that encapsulates the WS's operations and data types. The generated code will be based on the WSDL (Web Service Description Language) file, which defines the WS.

Add Dependencies: In your Java project, you need to add dependencies for the WS client library and the JAX-WS API (if using CXF). For example: Maven: pom.xml file
     

org.apache.cxf

cxf-rt-frontend-jaxws

3.2.6

junit

junit

4.12

test

Gradle: build.gradle file
     dependencies {

implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.2.6'

testImplementation 'junit:junit:4.12'

}

Create the WS Client: Use the generated stub code to create a Java class that extends the generated Service class. This client will use the WSDL file to connect to your WS. Invoke the WS Operations: In your Java client, you can now invoke the WS operations using the methods provided by the generated stub code. For example, suppose you have a WS operation named getPersonDetails that takes a String parameter and returns an instance of Person:
   Person person = personService.getPersonDetails("John Doe");

Here's a sample Java code using Apache CXF to consume a SOAP WS:

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import javax.xml.ws.*;

public class SoapClient {

public static void main(String[] args) throws Exception {

// Set the WSDL location and port number

String wsdlUrl = "http://localhost:8080/soap-web-service?wsdl";

int portNumber = 8080;

// Create a JAX-WS proxy (client)

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

factory.setServiceClass(PersonService.class);

factory.setAddress(wsdlUrl);

PersonService personService = factory.create();

// Invoke the getPersonDetails operation

Person person = personService.getPersonDetails("John Doe");

System.out.println("Got person details: " + person.getName());

}

}

In this example, you first set up the WSDL location and port number using the wsdlUrl variable. You then create a JAX-WS proxy (client) using the JaxWsProxyFactoryBean. Finally, you invoke the WS operation using the generated personService instance.

Remember to replace the placeholder values with your actual WS endpoint URL, username, and password (if required).

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.