simplo
simplo

Reputation: 111

Java Generate SOAP Envelope

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 !

Upvotes: 7

Views: 39160

Answers (2)

Koitoer
Koitoer

Reputation: 19533

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");

Upvotes: 13

Hubschrauber
Hubschrauber

Reputation: 101

You could grab the wsdl from the soap service (usually something like http://endpointurl?wsdl), and then use Apache CXF's wsdl2java utility to generate code with the -client parameter. The generated code will do a lot of the work for you in terms of building a valid SOAP request and sending it to the endpoint, or if you just want to see how it works, you could follow the calls it makes into the CXF source and see how they're doing things.

http://cxf.apache.org/docs/how-do-i-develop-a-client.html

Upvotes: 0

Related Questions