How to consume web services in Grails

Print Friendly, PDF & Email

Frankly, implementing RESTful API endpoints/services is a vital part of any modern web-based applications these days because of inherent advantages of this technology. However, how do you consume RESTful services if you are a client/user? There are several approaches to allow us fetch data from servers where they support programmatic access such as using HTTP libraries and requests.

In this post, I am going to present an approach of retrieving API endpoints in Grails based applications using rest-client-builder plugin. This plugin provides an alternative REST client implementation based on Spring’s RestTemplate that is not tied to commons-http-client, a library to allow Java developers to drag database from API based endpoints. If you would like to play with http client based route, please consult the alternative solution to this no longer maintained library.

Notes: The illustrated and introductory project was built in grails 2.5.6.

If you read through the plugin documentation, you should know the following line to be added to plugin section of BuildConfig.groovy file.

compile ":rest-client-builder:2.1.1"

The project includes a controller named SearchController and a service called SearchService. To demonstrate how the plugin supports, I implemented a method called retrieveBioModelsAllCuratedModels to fetch all manually curated models from BioModels repository which the service was fully supported. If you want to learn more about accessing data from BioModels programmatically, please consult their documentation page.

A presentation of retrieving data from BioModels repository
A presentation of retrieving data from BioModels repository

Below is a sample snippet to retrieve data with GET protocol.

JSONElement retrieveBioModelsAllCuratedModels() {
        final String BM_SEARCH_URL = ""
        String queryURL = """\
${BM_SEARCH_URL}&query=*:* AND curationstatus:\"Manually curated\" AND NOT isprivate:true&format=json"""
        RestBuilder rest = new RestBuilder(connectTimeout: 10000, readTimeout: 100000, proxy: null)
        def response = rest.get(queryURL) {
        if (response.status == 200) {
            return response.json
        return null

To display the result returned from BioModels, I called the service above directly in index action although it isn’t good practice. I recommend you creating a specific action in SearchController to call business logics implemented in your services. The result as you see in SearchService is a JSONElement so that I just render as it is. Below is a screenshot of the result prettify with JSON Editor Online.

A screenshot of JSON returned from BioModels
A screenshot of JSON returned from BioModels of the query trying to fetch all manually curated models

Anyone is interesting in using this route to access programmatically data from remote resources, the sample project has been pushed to bitbucket.

Hopefully, these tricks would bring a convenient approach to consume API endpoints in your Grails applications.

All constructive comments are welcomed. If you are willing to contribute financial support for our website, please follow the instructions below.

3 thoughts on “How to consume web services in Grails

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.