Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

Grails + CXF Example

Ok, we've been trying to get full fledged web services (providing) working in Grails for quite a while, it felt like it should be easy, as all the bits were there, but all of the plugins to date were more proof of concept vs Enterprise ready (e.g. no control over namespaces).

So, the target was to get CXF running with the following key requirements:

  • WSDL First
  • Minimal change required to CXF generated interface classes
  • Bound to Grails services so we can re-use business logic and interact with domain classes

Step 1:  Grails App + CXF

Create a simple Grails application (working from the ground up is the best way to explain it, rather than trying to explain our existing application), create a simple domain class, controller and service.

Download Apache CXF (I used 2.1): and extract somewhere on your PC.

Copy the following libraries from the CXF Installation over into your Grails application lib folder:


Next, add a resources.xml file to your grails-app/conf/Spring folder:

Next, install templates for your app (grails install-templates), then go into src/templates/war and edit web.xml, add the following:

Next, ensure that you turn off the inbuilt URL Mapping for the /ws/* in conf/UrlMapping.groovy:

Ok, now you should be able to start up your application (do this just to make sure you haven't broken anything).

Step 2:  WSDL First CXF Service

Now, grab a WSDL, in this case I will use currencyConverterService.wsdlthat comes with one of the many services frameworks I downloaded recently.  Go into the bin folder of the Apache CXF folder (where you saved it), and run the following command:

This generates an interface, an implementation and classes for all of the types - all properly marked up and configured.  Copy everything generated into the src/java folder in your Grails app (from the com folder up).

Now comes the fun bit!

Step 3:  Bind the CXF Service into your Grails Application

Ok, now what we need to do is to tell our Grails application that we have a service, and more importantly bind that service into an existing Grails service.

First we need to edit our Grails service, so that it implements the interface generated by CXF, and then add the methods of that interface with some code:

Ok, good so far.  Next step is to do a very small edit to the CXF Service Implementation to do the following things:

  1. We need to add a placeholder so that we can get Spring to inject the Grails Service into this class so that we can pass through the requests from the CXF class to the Grails service.
  2. Add code to the service method so that it passes through to the Grails Service

The complete code is below:

Next, we need to go back to our resources.xml and tell CXF that we now have a service, and provide the magic that injects the Grails service into the CXF one to link them together (add the following code inside the <beans> element of the file, under the CXF imports:

The key thing here is the bean creation, that injects the link to the Groovy Service (which is just another Bean once Grails is running) into the groovyObject property of our CXF service.  The other piece is just the simple method of creating a CXF service.

It is possible (in some cases - I haven't figured out why it works sometimes), to not use the intermediate Java Impl class, but rather configure the CXF Service to point straight at the Grails Service bean (provided it implements the interface). This makes it much faster, as you don't need to change the generated code at all (ideal).

In this case, the resources.xml would not have the <bean ...> and <ref bean="currencyConverter" /> becomes <ref bean="converterService"/>.  Further exploration may explain why it works sometimes but not others (I get a strange error during startup of a Null parameter to CXF).

Step 4:  Test!

When you run the application, you should see something like this appearing in the log:

Then, when you browse to:  http://localhost:8080/wstest/ws/services you should get the list of services (in this case just one), and then be able to view the generated WSDL (which should have all the same namespaces etc. as the original).

Here are some pictures to prove that it works (smile)


Next ... ?

  • No idea if this is the best way to do it, or if it will cause issues later on with respect to trying to use any of the more complex features of CXF. 
  • Also haven't done much testing to make sure it doesn't break other plugins, and we can still deploy it to OC4J with all the new libraries

Clearly, any comments or feedback welcome!

  • No labels