Send Soap request with Java
11,331
Below there's a demo of how you could do it. Basically you call addChildElement
and addTextNode
for each element you need.
Make sure you change the endpoint URL and SOAP Action in the main
method before calling.
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testing
public static void main(String args[]) {
String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx"; // CHANGE ME
String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit"; // CHANGE ME
callSoapWebService(soapEndpointUrl, soapAction);
}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "tem";
String myNamespaceURI = "http://tempuri.org/";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("pay", myNamespace);
SOAPElement merchantId = soapBodyElem.addChildElement("merchantId", myNamespace);
merchantId.addTextNode("7507231");
SOAPElement branch = soapBodyElem.addChildElement("branch", myNamespace);
branch.addTextNode("Licensed Branch Name");
SOAPElement alias = soapBodyElem.addChildElement("alias", myNamespace);
alias.addTextNode("Service alias Name");
SOAPElement paymentId = soapBodyElem.addChildElement("paymentId", myNamespace);
paymentId.addTextNode("merchants payment idetificator");
SOAPElement data = soapBodyElem.addChildElement("data", myNamespace);
SOAPElement dataParam = data.addChildElement("param", myNamespace);
SOAPElement dataParamKey = dataParam.addChildElement("key", myNamespace); dataParamKey.addTextNode("account");
SOAPElement dataParamValue = dataParam.addChildElement("value", myNamespace); dataParamValue.addTextNode("account cridentials");
SOAPElement hash = soapBodyElem.addChildElement("hash", myNamespace);
hash.addTextNode("?");
}
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 soapRequest = createSOAPRequest(soapAction);
SOAPMessage soapResponse = soapConnection.call(soapRequest, 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;
}
}
Related videos on Youtube
Author by
BekaKK
Updated on June 04, 2022Comments
-
BekaKK almost 2 years
I never used Soap. I searched and found some examples
My goal is to send like this Soap request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <soapenv:Header/> <soapenv:Body> <tem:pay> <tem:merchantId>7507231</tem:merchantId> <tem:branch>Licensed Branch Name</tem:branch> <tem:alias>Service alias Name</tem:alias> <tem:paymentId>merchants payment idetificator</tem:paymentId> <tem:data> <tem:param> <tem:key>account</tem:key> <tem:value>account cridentials</tem:value> </tem:param> </tem:data> <tem:hash>?</tem:hash> </tem:pay> </soapenv:Body> </soapenv:Envelope>
Can anyone tell me how I can send like this Soap request? or give me a examples or tutorials to send a soap like this. Thanks everyone
-
royalghost about 6 yearsYou can use SOAPUI as an IDE to create a SOAP Request and send to the server (or endpoint) - soapui.org
-
BekaKK about 6 yearsI know this is a same question ,but How I can create like this Soap request ? @Santosh Joshi
-
BekaKK about 6 yearsIs it a possible to create java code with this program ? @Prabin Paudel
-
Santosh Joshi about 6 yearsIf you have a WSDL. then you can use
WSDL2Java
to generate java code -
royalghost about 6 yearsTake a look at this - soapui.org/soapui-projects/…, First you need to make sure that you can consume the service before writing the code (kind of TDD) and then you can write the client. In order to generate client, you can use Eclipse and create project such as Web Service Client in order to generate the Java Project
-