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.

Comments:

You could cut it down a bit thusly:

String setSearchString(searchString) {
return proxy.GetSpeech(searchString)
}

Posted by Tom Wheeler on July 23, 2008 at 03: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 05:42 AM 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 04: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 04:51 PM PDT #

You can also remove 'return':)

String setSearchString(searchString) {
proxy.GetSpeech(searchString)
}

Posted by Alex Tkachman on July 24, 2008 at 12: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 04: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 24, 2008 at 07:12 PM 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 24, 2008 at 07:22 PM 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 24, 2008 at 07:38 PM PDT #

http://www.propaganda-web.com/design/gcom285/mens.htm 男性下着のことならプロパガンダ。他にも女性下着、ビキニ、マイクロボクサー、トランクスからセクシー系まで。パンツのことならお任せください。"

Posted by 男性下着 on August 14, 2008 at 04:43 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today