Thursday, September 18, 2008

Stand-alone Java Client using Eclipse and JBoss

Why This Blog

There are already a number of blogs, tutorials, how-to guides available which shows how to create JAX-WS web services. But when I was trying to develop a simple Hello web service today using JBoss and eclipse ganymede, I faced a lot of issues because none of them have explicit instruction for Eclipse and JBoss combination (I was specifically intereseted in playing around with JAX-WS annotations and their implementation by JBossAS4.2). Creating the service was quite straightforward (I actually followed this tutorial for that: http://www.javabeat.net/articles/40-creating-webservice-using-jboss-and-eclipse-europa-1.html) but I spent a lot of time in creating the client for it. So, I thought that I will put this post up so that someone else can benefit from my efforts :)

Assumptions
  • You are familiar with Java, JBoss and Eclipse
  • You have basic understanding of WebServices
  • You have created and deployed the WebService , using the tutorial mentioned above, in JBoss AS 4.2.x
Stand-alone Java Client using Eclipse
  1. Create a new "Java Project" in Eclipse. Let's name it "TestWsClient".

  2. Create the WebService Stubs using JBoss tool wsconsume. This is available in jboss_home/bin.
    wsconsume -k http://localhost:8080/FirstWebService/TestWs?wsdl

    This will generate the stubs in the jboss_home/bin/output directory. The following files were generated by the above command:
    • ObjectFactory.java
    • package-info.java
    • SayHello.java
    • SayHelloResponse.java
    • TestWs.java
    • TestWsImplService.java

  3. Copy contents of jboss_home/bin/output directory in the newly created java project under "src" folder. Your "Project Explorer" should look something like this (click on the image to see the full version):

    In this project all the files in the package com.shal.test.service were generated by the wsconsume utility.

  4. Now, the next step is to implement the client. Here is the sample code which I used for my test client (TestWsClient.java -- It is really simple and does nothing but hey, I just wanted to build a WebService client right!):

    package com.shal.test.client;

    import javax.xml.ws.WebServiceRef;

    import com.shal.test.service.TestWs;
    import com.shal.test.service.TestWsImplService;

    public class TestWsClient {
    @WebServiceRef (wsdlLocation="http://localhost:8080/FirstWebService/TestWs?wsdl")
    static TestWsImplService service;

    /**
    * @param args
    */
    public static void main(String[] args) throws Exception {
    service = new TestWsImplService();
    TestWs test = service.getTestWsPort();
    System.out.println(test.sayHello("Shalandra"));
    }

    }

  5. Now comes the step which ate a lot of my "precious" time :) i.e. figuring out which all jar files are required to compile and run this example. After spending a lot of time, I came up with the following list and was able to run my test client successfully:

    Jars required for compilation:
    • jaxb-api.jar
    • jbossws-native-jaxws.jar

    Jars required at runtime:
    • activation.jar
    • commons-logging.jar
    • concurrent.jar
    • javassist.jar
    • jaxb-impl.jar
    • jboss-common-client.jar
    • jboss-remoting.jar
    • jbossws-common.jar
    • jbossws-native-client.jar
    • jbossws-native-core.jar
    • jbossws-native-jaxrpc.jar
    • jbossws-native-jaxws-ext.jar
    • jbossws-native-saaj.jar
    • jbossws-spi.jar
    • jboss-xml-binding.jar
    • log4j.jar
    • mail.jar
    • policy.jar
    • stax-api.jar
    • wsdl4j.jar
    • xercesImpl.jar
    • xmlsec.jar

  6. Disclaimer: These were the jar files which were required for this simple client. If you try to do something more advanced (as in parse the response using a XML parser or something like that), you might need to add more jar files accordingly.

  7. So, add all these jar files to your project's classpath. These jar files are available in one of the following directories:
    • jboss_home/client
    • jboss_home/lib
    • jboss_home/lib/endorsed
    • jboss_home/server/default/lib

  8. Your final project structure should look something like this:
The WebService Code

For your reference, here is the implementation of WebService which I created and deployed in my JBoss server before implementing the Client code above.
package com.shal.test.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(name = "TestWs")

@SOAPBinding
(
style = SOAPBinding.Style.DOCUMENT,
use = SOAPBinding.Use.LITERAL,
parameterStyle = SOAPBinding.ParameterStyle.WRAPPED
)

public class TestWsImpl implements TestWs {

@WebMethod
public String sayHello(@WebParam(name="name")String name) {
return "Hello " + name + "!";
}

}

No comments: