Consuming a Web Service from Your Java Class
Wednesday, September 30th, 2009| To use a web service over a network, which is called “consuming” a web service, you need to create a web service client. For the creation of web service clients, we have various methods, we can generate the client JAR by using IDE’’s build functionality. IDE’’s like Netbeans,MyEclipse are having good support for doing these operations. | |
| If you decided to do it on your own, still we have the method to generate the client jar and import in your application. In this example i have used clientgent task from Weblogic, By using an simple ant script we can achive this. | |
The clientgen task(cutom ant task from weblogic) is available in Weblogic 8.1 under bea\\weblogic81\\server\\lib\\webservices.jar. According to Oracle(BEA) clientgen does the followingThe clientgen Ant task generates, from an existing WSDL file, the client component files that client applications use to invoke both WebLogic and non-WebLogic Web Services. These files include:
. |
|
| For example we”ll take the SpellChecker Web service, which is offered by Cdyne. The WSDL is made available to all. From the WSDL we”ll be able to generate the classes for consuming the webserivce.The ant script i have used to build the client jar looks like the following | |
<?xml version="1.0" encoding="UTF-8"?> <project name="MyFirstWSClient" default="build-client"> <taskdef name="clientgen" classname="weblogic.ant.taskdefs.webservices.clientgen.ClientGenTask" /> <target name="build-client"> <clientgen wsdl="http://ws.cdyne.com/SpellChecker/check.asmx?wsdl" clientJar="C:\\SpellCheckerClient.jar" packageName="com.techmaddy.ws"/> </target> </project> |
|
| The above given ant script, will give you an client stubs by whcih you can make a request to a Webservice and get the results. | |
| Now we”ll write an stand alone java class, from that we”ll make a request an webservice using the above created client stubs. | |
package com.techmaddy;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.HandlerRegistry;
import weblogic.webservice.context.WebServiceContext;
import weblogic.webservice.context.WebServiceSession;
import weblogic.webservice.core.handler.WSSEClientHandler;
import weblogic.xml.security.UserInfo;
import weblogic.xml.security.wsse.Security;
import weblogic.xml.security.wsse.SecurityElementFactory;
import com.cdyne.ws.Check;
import com.cdyne.ws.CheckSoap;
import com.cdyne.ws.Check_Impl;
public class SpellChecker {
private Check spellService = null;
private CheckSoap soapPortType = null;
private synchronized Check getService() throws IOException {
spellService = new Check_Impl();
WebServiceContext context = spellService.context();
WebServiceSession session = context.getSession();
HandlerRegistry registry = spellService.getHandlerRegistry();
List list = new ArrayList();
list.add(new HandlerInfo(WSSEClientHandler.class, null, null));
registry.setHandlerChain(new QName("SpellChecker"), list);
UserInfo ui = new UserInfo("", "");
session.setAttribute(WSSEClientHandler.REQUEST_USERINFO, ui);
SecurityElementFactory factory = SecurityElementFactory.getDefaultFactory();
Security security = factory.createSecurity(null);
security.addToken(ui);
Session.setAttribute(WSSEClientHandler.REQUEST_SECURITY, security);
return spellService;
}
private CheckSoap getPort() throws ServiceException, IOException {
if (spellService == null) {
try {
soapPortType = getService().getcheckSoap();
}
catch (ServiceException e) {
throw e;
}
catch (IOException e) {
throw e;
}
}
return soapPortType;
}
public void checkTextBody() throws RemoteException, ServiceException, IOException {
com.cdyne.ws.DocumentSummary doc = getPort().checkTextBody("Minr pupppy iss Wrong", "0");
String allcontent = doc.getBody();
System.out.println("The Content is "+allcontent);
System.out.println("The misspelled word is "+doc.getMisspelledWordCount());
}
public static void main(String[] args) {
SpellChecker sc = new SpellChecker();
try {
sc.checkTextBody();
}
catch(IOException ee) {
ee.printStackTrace();
}
catch (ServiceException e) {
e.printStackTrace();
}
}
}
|
|
| The above class queries the WS and returns the result. But If we use the above class, we”ll get the following exception SOAP header Security was not understood | |
| The SpellChecker classes, getService() method will get changed like the following. Because this webserivce doesn”t require any authentication, So when we SOAP header, we”ll get the above said exeception. | |
private synchronized Check getService() throws IOException {
spellService = new Check_Impl();
return spellService;
}
|
|
The WEB-INF/lib should have the following JARS. ![]() |

















