X

Geertjan's Blog

  • July 23, 2008

Groovy Makes Web Services Embarrassingly Easy

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 11 )
  • Tom Wheeler Wednesday, July 23, 2008

    You could cut it down a bit thusly:

    String setSearchString(searchString) {

    return proxy.GetSpeech(searchString)

    }


  • Geertjan Wednesday, July 23, 2008

    Thanks Tom. I'm thinking of making the above scenario the basis of the "NetBeans Groovy Quick Start" for 6.5.


  • Dierk König Wednesday, July 23, 2008

    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


  • Geertjan Wednesday, July 23, 2008

    Hi Dierk! You can simply open any existing Grails project. Try it. Convention over configuration is really nice for IDE support.


  • Alex Tkachman Thursday, July 24, 2008

    You can also remove 'return':)

    String setSearchString(searchString) {

    proxy.GetSpeech(searchString)

    }


  • Martin Adamek Thursday, July 24, 2008

    @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.


  • kodeninja Friday, July 25, 2008

    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.


  • Geertjan Friday, July 25, 2008

    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.


  • kodeninja Friday, July 25, 2008

    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.


  • &#21513;&#21407;&#12288;&#12477;&#12540;&#12503; Thursday, August 14, 2008
  • &#30007;&#24615;&#19979;&#30528; Thursday, August 14, 2008

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


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.