How to Create a Web Service Client with Groovy and NetBeans Platform 6.8
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
- Creating the Groovy Client
- Setting Up the Application
- Creating a Simple Prototype
- Using NetBeans Platform Swing Components
- Wrapping Up
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:
- 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.
- Download the full GroovyWS JAR ("groovyws-standalone-xxx.jar") here and add it to the application's "lib" folder.
- 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.
- 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());
}
} - 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. - 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.





Comments
Cej Hah replied on Mon, 2009/12/14 - 2:40pm
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