Close

March 11, 2010

Dead Simple Java Web Services and Clients

If you are like me and have seen a ton of languages out there provide web services with a few simple instructions (Perl / PHP / Python), you may have the impression that it should be pretty easy in Java? Couple of lines of code and voila a server. A few more lines and voila a client that consumes said service. Unfortunately that does not seem to be the case in my experience. WDSL setup this…wimport that (but only with JDK’s greater than 1.6_05..).

Here is a dead simple way to setup clients and services in a few steps.

Creating a Web Service in Groovy

1.) Go and GET GROOVY NOW. Install it for your platform and get it working on the command prompt so you can type “groovy” and see a list of options. (Side note: These demos are borrowed from their help guide..)

2.) Create two files in your directory.

MathService.groovy

class MathService {
  double add(double arg0, double arg1) {
    return (arg0 + arg1)
  }
  double square(double arg0) {
    return (arg0 * arg0)
  }
}

test.groovy

import groovyx.net.ws.WSServer
def server = new WSServer()
server.setNode("MathService", "http://localhost:6980/MathService")
server.start()

3.) In a command prompt in the directory of those files, type: “groovy test.groovy”.
Voila, webservice is running. Groovy takes care of ALL of the evil details for your basic web-service. Just like it ought to be.

Creating a Web Service in Groovy
1.) Add the following to the file specified below:

clientTest.groovy

import groovyx.net.ws.WSClient
def proxy = new WSClient("http://localhost:6980/MathService?wsdl", this.class.classLoader)
proxy.initialize()
def result = proxy.add(1.0 as double, 2.0 as double)
//assert (result == 3.0)
print "\nResults is "+result
result = proxy.square(3.0 as double)
print "\nResults is "+result

The Groovy client can now chat with the Groovy Web service. Now lets step it up a bit. Let’s call our web-service from good old java itself. You can get your PHD in consuming Java Soap web-service envelopes OR follow the next few steps.

1.) Download the latest Java JDK with NetBeans (http://java.sun.com/javase/downloads/index.jsp). Make sure its at least JDK 6 Update 6. I’ll save you the glory details, but apparently there was an update to Java’s backend web service client (which Groovy already appears to use). If you download the latest version, you won’t have any issues.

2.) Fire netbeans up. Switch to the “Services” tag on the top left hand side (next to Files).

3.) Expand webservices. Right click on it and click “Add webservice”.
For the URL simply give it the location of your Groovy webservice (http://localhost:6980/MathService?wsdl).

4.) It should build and complete. If you expand it out you’ll see the functions “add” and “square”…Piece of cake.

5.) Now create a boring old java application to call your service. (File -> New Project -> Java -> Java Application).

6.) It will create all of the fun Main class stuff. You can run it to test it to make sure it compiles. (F6).

7.) Now the magic…Switch back to the “Services Tab” on the left. Go down to your “MathServicePort” service to the “square” method.
DRAG the function name INTO your “main” static function. Netbeans handles ALL of the heavy lifting for you.

You should see something like this to run.

package javaclient;
/**
 *
 * @author Jeremy Silva
 */
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            double arg0 = 100.0;
            defaultnamespace.MathService service = new defaultnamespace.MathService();
            defaultnamespace.MathServicePortType port = service.getMathServicePort();
            // TODO process result here
            double result = port.square(arg0);
            System.out.println("Result = " + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

8.) Run the code and it should spit out something like:
Result = 10000.0

This just a bare-bones recipe to get started. There are a ton more options to play around with in both the client and the web-service. Groovy, in my opinion, has significantly lowered the bar for new developers to the java language. It covers up some of the messy details of java programmers, that may be put off by java coming from other scripting languages (Python/Perl.. Php). It does however allow you access to the nuts and bolts of Java when you really need it.

Netbeans too has been a fantastic help in being able to plug-n-play some of the more complicated architecture items of Java.

I would like to thank the developers in both of these projects, for continuing to make development a fun job!