Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create a Web Service Client with Groovy and NetBeans Platform 6.8

12.14.2009
| 49259 views |
  • submit to reddit

In this article you will learn how to leverage the best of Groovy together with the best of the NetBeans Platform. Groovy will be used to access a Shakespeare quotation web service and parse its payload. The NetBeans Platform will be used to create a modular GUI in Swing for interacting via Groovy with the web service.

During the course of this article, you will be introduced to many NetBeans API classes, such as TopComponent, Node (specifically, BeanNode and AbstractNode), BeanTreeView, ExplorerManager, ChildFactory, ProgressHandleFactory, and ModuleInstall. Of central concern throughout the scenario that follows will be the NetBeans API classes Lookup, LookupListener, and, in particular, InstanceContent. While by no means a requirement, it will be of benefit to you to watch the screencast series Top 10 NetBeans APIs before continuing with the rest of this article.

Contents


Note: If, for some reason, the Shakespeare quotation service is unavailable at the time you're working through this article, simply find a different web service somewhere on-line and then use that instead, adapting the instructions below where necessary.

At the end of this article, you will have an application that looks like this:

And, by downloading and adding one new NetBeans module, the OfficeLAF module, and without any configuration or coding on your part, you'll be able to deliver an application that looks like this:

If you get stuck during the course of this article, the source code for the application can be found here:

http://kenai.com/projects/shakespeareannotater/sources/subversion/show

Creating the Groovy Client

In this section, we create a Java application that uses Groovy to access and parse our web service. The Java application will have this structure:

Take the following steps:

  1. Create a new Java application, named "ShakesGroovy". Use the New File dialog in the IDE to add a new Groovy class named "ShakesWsClient.groovy" to the application. When you do this, the IDE adds the "groovy-all.jar" to the classpath of the application and updates the build script to enable the Groovy class to be compiled to Java when you build the application.

  2. Download the full GroovyWS JAR ("groovyws-standalone-xxx.jar") here and add it to the application's "lib" folder.

  3. Define the Groovy class as follows:
    package shakesgroovy

    import groovyx.net.ws.WSClient

    class ShakesWsClient {

        String play, speaker, words

        void findQuote(searchString){
            def proxy = new WSClient("http://www.xmlme.com/WSShakespeare.asmx?WSDL",
                 ShakesWsClient.class.classLoader)
            proxy.initialize()
            def speech = new XmlParser().parseText(proxy.GetSpeech(searchString))
            play = speech.PLAY.text()
            speaker = speech.SPEAKER.text()
            words = speech.text()

        }

    }

    In the IDE's Groovy editor, the above should look as follows:

    Note: For information about GroovyWS, see http://groovy.codehaus.org/GroovyWS for details.

  4. Define the "Main.java" class (in the image above, it is named "Demo.java") like this, so that you can try out the Groovy web service client above:
    package shakesgroovy;

    public class Demo {

    public static void main(String[] args) {
    ShakesWsClient client = new ShakesWsClient();
    client.findQuote("fair is foul");
    System.out.println(client.getPlay());
    System.out.println(client.getSpeaker());
    System.out.println(client.getWords());
    }

    }
  5. Run the application. You should see something like this as the application's output:
    12 Dec 2009 6:45:47 PM org.apache.cxf.endpoint.dynamic.DynamicClientFactory outputDebug
    INFO: Created classes: com.xmlme.webservices.GetSpeech, com.xmlme.webservices.GetSpeechResponse, com.xmlme.webservices.ObjectFactory
    12 Dec 2009 6:45:49 PM groovyx.net.ws.AbstractCXFWSClient getBindingOperationInfo
    WARNING: Using SOAP version: 1.1
    MACBETH
    ALL
    Fair is foul, and foul is fair: Hover through the fog and filthy air.
  6. Look in the Files window, specifically in the "dist" folder, which you can see in NetBeans IDE's Files window. You should see the two Groovy JARs, as well as the JAR containing the Java classes produced by the Groovy commpile process:

As will be explained in the next sections, the three JARs that you see above will need to be wrapped in a NetBeans module, which you'll include in the NetBeans Platform application that you'll create in the next sections. Then you'll create a new module, which will provide a new window in the application, which will access the Java code (compiled from Groovy above) to interact with the Shakespeare web service. That will be possible because of the dependencies you will set between the two modules.

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Cej Hah replied on Mon, 2009/12/14 - 2:40pm

Geeez, it's almost 2010, can't we get away from Big Web Services already?!  REST is your friend.  SOAP is your enemy.

Ning Sun replied on Wed, 2009/12/16 - 3:49am

Really nice article.

I have a question about the code when binding mouse listener to bean tree view. That is step 8 in "Using NetBeans Platform Swing Components", line 15. How you got the reference of resultPane in MyBTV? Is there any trick?

Thank u for your great article.

 

 

Sven Alfred replied on Thu, 2009/12/31 - 3:38pm

Great walkthrough. SOAP or not... doesnt matter.

Combining the power of the platform with the simplicity of Groovy. I will use this as recipe for my own platform experiments.

cheers

Sven

Jeff Schwartz replied on Fri, 2010/01/29 - 9:16am

@Geertjan,

Very groovy to see these examples in Groovy. IMO Groovy is the gem of the Java virtual machine. I find myself using it a lot. I fall back to Java when I can find a legitimate reason to, such as when addressing critical performance issues. Otherwise, Groovy just makes more sense to me.

A lot of the developers I speak with believe Oracle's purchase of Sun doesn't bode well for Netbeans. Nothing that I have read leads me to believe that Oracle intends to throw their 'official' support behind it. Maybe now is time to set Netbeans free from any single corporate sponsor and before Oracle kills it with neglect.

 Be well.

 Jeff @ http://jeff-schwartz.blogspot.com/

Mateo Gomez replied on Tue, 2012/04/17 - 12:30am

 You just made my life easier

 

 mexican dessert recipes

Matt Coleman replied on Tue, 2012/04/17 - 12:43am

 

 this makes want to get to know netbeans...great job you guys

graphic design buffalo

Eugenios Ampelius replied on Mon, 2013/09/30 - 10:49am

 Groovy does a great job. What's the deal with REST, though? I haven't used it. Is it really better than SOAP? Thank you.

Cata Nic replied on Mon, 2013/09/02 - 4:10am

 The platform can be extended to something custom and dedicated do the client's needs ?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.