Wednesday July 23, 2008
Groovy Makes Web Services Embarrassingly Easy
Meera pointed me to another very cool article she's written, RESTful Web Services in 60 Seconds, and (because I've been in an intensely groovy frame of mind for a few days now), I immediately associated it with my earlier experiments with Groovy and web services, in a blog entry entitled Groovy Web Service from the end of last year, based on some key learnings from the official Groovy web service site, (which has expanded a lot since then, I noticed today). At the time, I needed to hack things quite a bit to get web services to work with Groovy in NetBeans IDE (as honestly recorded in that blog entry).
So, I thought, how do things stand today in terms of Groovy and web services in NetBeans IDE? To be perfectly honest, the improvement couldn't have been much better, aside from the currently incomplete code completion (which is a work in progress still). In fact, I was able to mix and match Matisse with a Groovy web service, although you can't tell from the result:
This is the actionPerformed in the JButton in the Matisse form that you see above:
ShakesWSClient shakes = new ShakesWSClient();
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String searchString = shakes.setSearchString(searchTextArea.getText());
resultTextArea.setText(searchString);
}
And this is the ENTIRE web service client:
package org.me.hello
import groovyx.net.ws.WSClient
class ShakesWSClient {
def proxy = new WSClient("http://www.xmlme.com/WSShakespeare.asmx?WSDL", ShakesWSClient.class.classLoader)
String setSearchString(searchString) {
def newQuote = proxy.GetSpeech(searchString)
return newQuote
}
}
Really, that's just a bit ridiculous. And I'm sure that the Groovy experts could cut the above code down a few lines and characters further. And I didn't need to hack anything in NetBeans. The Groovy class behaved seamlessly with the Java class.
Jul 23 2008, 09:33:20 AM PDT Permalink
You could cut it down a bit thusly:
String setSearchString(searchString) {
return proxy.GetSpeech(searchString)
}
Posted by Tom Wheeler on July 23, 2008 at 10:51 AM PDT #
Thanks Tom. I'm thinking of making the above scenario the basis of the "NetBeans Groovy Quick Start" for 6.5.
Posted by Geertjan on July 23, 2008 at 12:42 PM PDT #
Cool, man!
The Groovy/Grails Support in NB 6.5 is going to be hit and your posts perfectly show the benefits it brings!
BTW: it would be helpful if it would be possible to create a Groovy or Grails NB project "from existing sources" (like for Java projects). With that, I could more easily use NB for my existing projects.
keep the posts coming
Dierk
Posted by Dierk König on July 23, 2008 at 11:47 PM PDT #
Hi Dierk! You can simply open any existing Grails project. Try it. Convention over configuration is really nice for IDE support.
Posted by Geertjan on July 23, 2008 at 11:51 PM PDT #
You can also remove 'return':)
String setSearchString(searchString) {
proxy.GetSpeech(searchString)
}
Posted by Alex Tkachman on July 24, 2008 at 07:52 AM PDT #
@Dierk: As Geertjan said, you can open any existing Grails application without any 'import' action and there's no Groovy application project, just regular Java projects, and that one already has import.
Posted by Martin Adamek on July 24, 2008 at 11:28 AM PDT #
Hi Geertjan!
I created a simple Java project in my NetBeans 6.5m1. I then created a simple JFrame to display the speeches and a Groovy class which handles the Web Service part. I explicitly set, as a JAR dependency, the "groovyws-standalone-0.3.1.jar" file. After building the project, when I tried to run it, I got the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/tools/ant/types/DirSet
at groovyx.net.ws.WSClient.<init>(WSClient.java:199)
Did you encounter such an error? What could be missing here?
Also, although the Groovy Plugin Options dialog allows specifying the Groovy Home folder, it uses a pre-specified groovy jar only (groovy 1.5.5). Why is that so?
Thanks.
Posted by kodeninja on July 25, 2008 at 02:12 AM PDT #
You need to add the "ant.jar". I don't know why exactly. You can find it in the NetBeans installation directory, within the folder java2/ant/lib.
Posted by Geertjan on July 25, 2008 at 02:22 AM PDT #
Never mind, i got it working :)
It needed the 'ant' jar to be on the classpath, as it seems the "groovyws" references it indirectly.
Also, it's better to use JDK 1.5 as the groovyws seems to ask for JAXB 2.1 while the JDK 1.6 has 2.0.
Otherwise you keep getting linkage errors!
Thanks.
Posted by kodeninja on July 25, 2008 at 02:38 AM PDT #
<a href="http://fuzokudx.com/soap/">吉原 ソープ</a>今までの吉原にはまったくない、新しい<a href="http://fuzokudx.com/soap/">吉原 ソープ</a>のお店です。お客様に提供するのは、究極の癒しとリラクゼーション<a href="http://fuzokudx.com/soap/">吉原 ソープ</a>です。今まで<a href="http://fuzokudx.com/soap/">吉原 ソープ</a>に縁の無かったお客様のニーズにお応えします。
http://www.propaganda-web.com/design/gcom285/mens.htm 男性下着のことならプロパガンダ。他にも女性下着、ビキニ、マイクロボクサー、トランクスからセクシー系まで。パンツのことならお任せください。"


