Thursday, December 07, 2006

Basic web service implementation

This post describes how to implement a simple webservice using Weblogic, Eclipse, and Apache Axis. We will go through these steps to implement the Webservice
  1. Enable Weblogic domain for WebServices
  2. Create a simple web service class.
  3. Auto-generate WebServices classes in Eclipse
  4. Auto-generate the classes for calling the web service.
  5. Create a Client Application
  1. Enable Weblogic domain for WebServices: Follow these steps to enable your weblogic domain for web services.
    1. Start the Configuration Wizard (c:/bea9.2/weblogic92/common/bin/config.cmd)
    2. In the Welcome window, select Extend an Existing WebLogic Domain. Click Next.
    3. Select the domain to which you want to apply the extension template. Click Next.
    4. Select Extend My Domain Using an Existing Extension template.
    5. Enter the following value in the Template Location text box: WL_HOME/common/templates/applications/wls_webservice.jar. Click Next.
    6. Select no. Click next.
    7. Verify that you are extending the correct domain, then click Extend.
  2. Create a simple web service class: Create an Eclipse Web Project. The reason for using a Web Project is that the J2EE specification requires that for implementing you need one of the following.
    • A Java class running in the Web container.
    • A stateless session EJB running in the EJB container.
    Create the following class within the web application.
    public class TestService {
    public String sayHello(String message) {
    System.out.println("sayHello:" + message);
    return "You said '" + message + "'";
    }
    }
    TestService.java
  3. Auto-generate WebServices classes in Eclipse: Right-click on the TestService.java class and select Web Services->Create Web Service. If you have a weblogic server defined in eclipse, then the Web service will be automatically published to the server. Make sure that your server definition looks like this.

  4. Auto-generate the classes for calling the web service: Create a Java application and copy the generated WSDL file from the web project. Right-click on the WSDL file and Web Services->Generate client. All the required classes will be generated, and the jar files will also be imported.
  5. Create a Client Application: We will use a simple Java client for this web service. The following is the code for the client application.
    import java.rmi.RemoteException;
    import service.TestService;
    import service.TestServiceProxy;
    public class SimpleClient {
    public static void main(String[] args) {
    TestService service = new TestServiceProxy();
    try {
    System.out.println(service.sayHello("Hello"));
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    }
    }
This example was run on Weblogic 9.2, Eclipse Callisto 3.2, Java 5.0.

7 comments:

  1. I am getting a java classnotfound exception while generating the wsdl:-

    IWAB0398E Error in generating WSDL from Java: java.lang.ClassNotFoundException: TestService
    java.lang.ClassNotFoundException: TestService
    at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:402)
    at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:347)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:83)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)

    ReplyDelete
  2. What version of eclipse/java and weblogic are you using?

    ReplyDelete
  3. Hi Abhi, I am working in weblogic 9.2 server. We are having an application which needs to access a Spring Bean from weblogic WebServices.

    So How I did that work was

    (1)I created weblogic webservice.
    (2)Created a Spring Bean which is having JDBC code which inserts some data into database.
    (3) I created an Interface (the method would be implemented in Spring Bean...)
    (4) I am using this spring Bean's Interface in weblogic WebService.For this I created a ApplicationContextFactory class.Which instantiate "applicationContext.xml file".
    so Once I get the instance of that ApplicationContext , then I am using the method "getBean("service"), to get the instance of the Interface.

    Then passing a String into that Interface method.

    Assumed that the flow would be like this.

    (a)webservice receives a String from client.
    (b)webservice get the Spring's Bean class using ApplicationContextFacotry.
    (c)pass the client send String as an argument to the Spring Bean's Method.
    (d) Which Spring Bean accepts and using the JDBC program it inserts into the database.

    But some when I run the WebService's from the Weblogic's workshop(9.2) and passing the a String a parameter to the webservice..it is giving an error as

    " Error invoking webservice.SpringService (POJO): java.lang.NoClassDefFoundError:ComponentHandler.handleRequest:115
    java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext: ComponentHandler.handleRequest:115
    java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
    at webservice.ApplicationContextFactory.getApplicationContext(ApplicationContextFactory.java:16)
    at webservice.SpringService.insert(SpringService.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at weblogic.wsee.component.pojo.JavaClassComponent.invoke(JavaClassComponent.java:99)
    at weblogic.wsee.ws.dispatch.server.ComponentHandler.handleRequest(ComponentHandler.java:64)
    at weblogic.wsee.handler.HandlerIterator.handleRequest(HandlerIterator.java:127)
    at weblogic.wsee.ws.dispatch.server.ServerDispatcher.dispatch(ServerDispatcher.java:85)
    at weblogic.wsee.ws.WsSkel.invoke(WsSkel.java:80)
    at weblogic.wsee.server.servlet.SoapProcessor.handlePost(SoapProcessor.java:66)
    at weblogic.wsee.server.servlet.SoapProcessor.process(SoapProcessor.java:44)
    at weblogic.wsee.server.servlet.BaseWSServlet$AuthorizedInvoke.run(BaseWSServlet.java:165)
    at weblogic.wsee.server.servlet.BaseWSServlet.service(BaseWSServlet.java:84)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3214)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)..."

    So please help me in this regard.

    Thanks!
    vis

    ReplyDelete
  4. hi.. i've just started with webservices... i am using eclipse to build a webservice.. i am not sure abt the role of a webservice client, as in how a client invokes a webservice in eclipse. you've shown a code for a client application but i don't understand where i am suppose to place this code.. it'll be great if you can help me..

    ReplyDelete
  5. How about building Java applications in a standard way - using Ant build scripts!

    ReplyDelete
  6. hi abhi,

    can you please tell me as how to load excel file data in Mysql using java?

    Thanks

    ReplyDelete
  7. Hi Abhi

    Do we have any good tutorial on

    Spring-MVC,Apache Axis and Hibernate

    Waiting for your earliest reply

    Thanks
    Selden

    ReplyDelete

Popular Posts