How to call web service from java code from standalone application, from Java Bean or from Servlet.
We have two ways:
Call web-service from standalone application (desktop) using SAAJ:
We will create SOAP message, using SAAJ, and then we will call web service.
1. Create SOAP message:
If you have web service operation getName and this operation has input structure like this:
<user>
<userId>1</userId>
</user>
Then we will create SOAPMessage like this:
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//create structure like our web service operation input
SOAPElement bodyElement = body.addBodyElement(envelope.createName(
"<web service operation name - getName>", "<prefix, I use 'ns1'>", "<uri, it is property 'targetNamespace' in annotation '@WebService' in your web service java class>"));
SOAPElement nodeUser = bodyElement.addChild("user");
SOAPElement nextNode = nodeUser.addChildElement("userId").addTextNode("1");
//if you should make authorization do this
MimeHeaders headers = msg.getMimeHeaders();
String upas = "login" + ":" + "password";
String auth = "Basic " + new String(javax.xml.bind.DatatypeConverter.printBase64Binary(upas.getBytes()));
headers.addHeaders("Authorization", new String(auth.getBytes()));
2. Now we can call web service with our SOAPMessage
URL url = new URL("<url to web service wsdl without '?wsdl', or url in web service wsdl in node 'address' in property 'location'>");
SOAPConnectionFactory cf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = cf.createConnection();
//here we call web service by java code
SOAPMessage replay = conn.call(msg, url);// replay contains web service response
conn.close();
We have two ways:
- For Java Bean or Servlet we use Eclipse (NWDS) to generate Java Class Client - Proxy Class.
- For standalone (desktop) we use SAAJ - SOAP with Attachments API for Java.
- Create EJB Module project in Eclipse (NWDS)
- Create Enterprise Application project in Eclipse (NWDS)
- Import here web-service WSDL
- Right click on WSDL -> Web Services -> Generate Client
- In pop-up window move slider to "Develop client" level. Check that "client project" and "EAR project" is yours. Go to next and then push finish.
- Eclipse has created package which contains your Proxy Class for your web-service. It's name looks like "<NameOfYourWebService> + Service" - remember that class.
- If you want to call web-service from Java Bean go to 8 step, if from servlet go to 9 step.
- Create Session Java Bean. In your Java Bean create parameter "webService" with type of your proxy class ( it's name "<NameOfYourWebService> + Service" - remember? ). Over that parameter specify "@WebServiceRef" annotation and set: "name=<NameOfYourWebService> + Service".
- Create Servlet. In your Servlet create parameter "webService" with type of your proxy class ( it's name "<NameOfYourWebService> + Service" - remember? ). Over that parameter specify "@WebServiceRef" annotation and set "name=<NameOfYourWebService> + Service".
- Now if you want call web-service from Servlet, J2EE engine will initialize our parameter "webService" with proxy class when it will be initialized. If you want call web-service from another Java Bean, you can use JNDI to get initialized proxy class. Do that like this: javax.naming.InitialContext.doLookup("<Java Bean JNDI Name of your Java Bean>");
- In Java Bean or Servlet create method where you want to call web-service.
- In parameter "webService" you have method "get + <NameOfYourWebService> + Port". Call that and you will get your web-service object. This object contains all operations of your web-service. Use it!
Call web-service from standalone application (desktop) using SAAJ:
We will create SOAP message, using SAAJ, and then we will call web service.
1. Create SOAP message:
If you have web service operation getName and this operation has input structure like this:
<user>
<userId>1</userId>
</user>
Then we will create SOAPMessage like this:
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
//create structure like our web service operation input
SOAPElement bodyElement = body.addBodyElement(envelope.createName(
"<web service operation name - getName>", "<prefix, I use 'ns1'>", "<uri, it is property 'targetNamespace' in annotation '@WebService' in your web service java class>"));
SOAPElement nodeUser = bodyElement.addChild("user");
SOAPElement nextNode = nodeUser.addChildElement("userId").addTextNode("1");
//if you should make authorization do this
MimeHeaders headers = msg.getMimeHeaders();
String upas = "login" + ":" + "password";
String auth = "Basic " + new String(javax.xml.bind.DatatypeConverter.printBase64Binary(upas.getBytes()));
headers.addHeaders("Authorization", new String(auth.getBytes()));
2. Now we can call web service with our SOAPMessage
URL url = new URL("<url to web service wsdl without '?wsdl', or url in web service wsdl in node 'address' in property 'location'>");
SOAPConnectionFactory cf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = cf.createConnection();
//here we call web service by java code
SOAPMessage replay = conn.call(msg, url);// replay contains web service response
conn.close();