6

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 !

simplo
  • 91
  • 1
  • 1
  • 10

2 Answers2

12

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");
Poger
  • 1,897
  • 18
  • 16
Koitoer
  • 18,778
  • 7
  • 63
  • 86
0

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

  • Thanks for your answer but i don't have a wsdl file. i will try the approach from Koitoer – simplo Feb 18 '15 at 11:12
  • If the SOAP service you are calling is published on an app server, it is often the case that you can download the wsdl by just adding "?wsdl" to the end of the base endpoint url that you use to call the service. Just FYI in case you're missing a chance to simply download a copy of the wsdl and avoid doing a lot of manual coding work. – Hubschrauber Feb 18 '15 at 20:29
  • Yes i know but this SOAP service don't have a wsdl that why i try to construct it myself to send it in the Http request. now i need to kwo how to put the constraint for the namespace like xsi:type="xsd:int. I search for it and found i have to use something like XMLType_INT. can you tell me please to use it ? Thanks for help – simplo Feb 19 '15 at 23:31