Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generate SOAP Envelope

Tags:

java

soap

i have the following method :

String[] getEmployeeDetails ( int employeeNumber ); The assosiate request look like this :

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
  <SOAP-ENV:Envelope
   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/1999/XMLSchema">
	<SOAP-ENV:Body>
		<ns1:getEmployeeDetails
		 xmlns:ns1="urn:MySoapServices">
			<param1 xsi:type="xsd:int">1016577</param1>
		</ns1:getEmployeeDetails>
	</SOAP-ENV:Body>
  </SOAP-ENV:Envelope>

this example come from this link [http://www.soapuser.com/basics3.html][1]

I don't understand how they do to generate it programtically with java. Please help !

like image 473
simplo Avatar asked Feb 17 '15 20:02

simplo


People also ask

How do you make a SOAP envelope in Java?

SOAPEnvelope envelope = soapPart. getEnvelope(); You can now use the getHeader and getBody methods of envelope to retrieve its empty SOAPHeader and SOAPBody objects. SOAPHeader header = envelope.

How do I create a SOAP request in Java?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

How do I make a SOAP message?

Create a SOAP message to define the remote endpoint, WSDL, and authentication settings. Navigate to System Web Services > SOAP Message. Click New. Enter a Name to identify the SOAP message.


1 Answers

Basically you need to use SAAJ API, this is an API that use SOAPMessage and give you some objects and methods to create SOAP request programatically, you shouls see this link for further reference. Also review the documentation from Oracle, they give you some useful examples. For real example you could check this link

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();

// Retrieve different parts
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();

// Two ways to extract headers
SOAPHeader soapHeader = soapEnvelope.getHeader();
soapHeader = soapMessage.getSOAPHeader();

// Two ways to extract body
SOAPBody soapBody = soapEnvelope.getBody();
soapBody = soapMessage.getSOAPBody();

// To add some element
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName  = soapFactory.createName("getEmployeeDetails","ns1","urn:MySoapServices");
SOAPBodyElement purchaseLineItems = soapBody.addBodyElement(bodyName);
Name childName = soapFactory.createName("param1");
SOAPElement order = purchaseLineItems.addChildElement(childName);
order.addTextNode("1016577");
like image 70
Koitoer Avatar answered Oct 25 '22 13:10

Koitoer