Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume an API in a grails application

Tags:

json

rest

grails

I have a service running on this url: http://localhost:8888

I get results from this service by invoking it like this:

http://localhost:8888/colors?colorname=red&shade=dark

and I get the results back in JSON like this:

 {
      "request#": 55,
      "colorname": "red",
      "shade": "dark",
      "available": "No"
 }

Question

What are some ways by which I can consume this service in my grails application?

like image 224
Anthony Avatar asked Oct 14 '13 19:10

Anthony


People also ask

How does grails framework work?

The Grails framework takes advantage of the Model-View-Controller (MVC) design pattern to partition responsibilities within the application and to simplify the application architecture. Model classes should represent the domain objects in your system. Controller classes should control the flow of the application.

Is grails a Java framework?

Grails is a Java-based web application framework that uses the Apache Groovy programming language.


1 Answers

Assuming all the configuration are there for rest client builder, you would end up with 2 lines of code consuming the service as:

//controller/service/POGO
def resp = rest.get("http://localhost:8888/colors?colorname=red&shade=dark")
resp.json //would give the response JSON

where

//resources.groovy
beans = {
    rest(grails.plugins.rest.client.RestBuilder)
}
like image 176
dmahapatro Avatar answered Sep 28 '22 00:09

dmahapatro