Monday, November 12, 2007

Simple web services using a Groovy MultiActionController in Spring

There have been many times I have wanted to create a simple XML API on top of my Spring applications. One technique I find myself using a lot is to create a scripted MultiActionController that uses the Groovy MarkupBuilder to render XML. (requires Spring 2.0 and above)

So as an example, Lets assume we have a Spring application created that is for an online store. (Download link for source code is below) We want to provide an xml representation of the products we offer from the following url:


This will provide an xml document that looks like the following:
<product id="1" name="Product 1" price="2.5"/>
<product id="2" name="Product 2" price="3.5"/>
<product id="3" name="Product 3" price="4.5"/>

So the first step to creating our xml api is to setup a dispatcher servlet. We will add the following servlet definition and mapping to the web.xml.


The next step is to create the dispatchers bean definition xml. This will be a file named api-servlet.xml that will be located in the WEB-INF folder of your application. The following are the contents of the api-servlet.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<prop key="/*">apiController</prop>

<lang:groovy id="apiController" script-source="/WEB-INF/groovy/APIController.groovy">
<lang:property name="productService" ref="productService" />


Next step is to create the scripted controller. This will be called APIController.groovy and located in the WEB-INF/groovy folder.

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.springframework.web.servlet.mvc.multiaction.MultiActionController
import groovy.xml.MarkupBuilder

class APIController extends MultiActionController {

def productService

void products(HttpServletRequest req, HttpServletResponse res) {
def products = productService.findAllProducts()
res.contentType = "text/xml"
def markupBuilder = new MarkupBuilder(res.writer)
markupBuilder.products {
for (p in products) {
product(,, price:p.price)


Thats it.. We can now deploy and access our web service. Additional methods could now be added to the APIController specified above to expose other data elements from the online store. One proposed method might be a product method that loads a particular product by id.

I have included a link to all the source code in a completed deployable web app in a maven project. It also takes advantage of the maven cargo plugin to support running of the application from within a container. All you have to do is have maven2 installed and run "mvn clean package cargo:start". Be patient it might take a while to fetch Tomcat the first time it runs.

Sample Project Structure:
|-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- acme
| `-- store
| |-- domain
| | `--
| `-- service
| |--
| `--
`-- webapp
| |-- api-servlet.xml
| |-- applicationContext.xml
| |-- groovy
| | `-- APIController.groovy
| `-- web.xml
`-- index.jsp

Dowload Sample Project Source Code


Graeme Rocher said...

Or alternatively you could use Grails (built on Spring MVC) and have a domain class:

class Product {

Then in your controller do:

render Product.list() as XML

Job done ;-)

Craig MacKay said...

Yep, I agree that it can't get any easier to render XML for your domain objects as it is with Grails. (or render JSON)

render Product.list() as JSON

I am a big fan of Grails. It provides the simplest way to produce Spring based applications and it can take advantage of all the capabilities/libraries Java has to offer.

This post is mainly targeting those who have existing Spring applications in place and want to introduce a little scripting to accomplish specific tasks like rendering xml services.