Groovy and Scala

Learn about REST in Groovy and Scala.

We'll cover the following

REST in Groovy

Groovy includes some support for XML, such as the XMLSlurper.

Grails includes converters for simply converting objects to XML or JSON or vice versa.

import grails.converters.JSON
import grails.converters.XML

 class BookController {
       def getBooks = {
            render Book.list() as JSON
       def getBooksXML = {
            render Book.list() as XML

For REST services that service multiple formats, we can use the built-in withFormat in Grails. So the above would become the following:

def getBooks = {
       withFormat {
           json { render list as JSON }
           xml { render list as XML }

Then Grails would decide which format to use based on numerous inputs; the simplest being the extension of the URL, such as .json.

For using web-services in Grails, there’s a rest plugin available.

REST in Scala

There are several options for writing REST web services in Scala. Scala itself provides first-level support for XML literals so you can write your own XML conversions.

Lift has built-in support for REST. Play provides built-in parsers for JSON and XML and a simple DSL for creating JSON.

For example:

Get hands-on with 1200+ tech skills courses.