Skip to content

This plugin add awesomeness to the default REST response of grails applications such as pagination, sorting and include/exclude fields

Notifications You must be signed in to change notification settings

ejaz-ahmed/grails-rest-awesome-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grails-rest-awesome-plugin - Sponsored by SaaSMax

This plugin adds pagination support to the default REST response of grails applications. I've plan to add search and include/exclude fields in future.

Installation

compile 'org.grails.plugins:rest-awesome:0.2.0'

Using this plugin

This plugin includes a sample application to which can also be referred.

In order to use this plugin, you will have to first register the custom marshaller provided by this plugin. If you have a domain class such as Book as shown below:

class Book {
    String title
    Double price

    static constraints = {
    }
}

You have to register two marshallers for this class (one for collection and one for individual item)

To do so, add following lines to your grails/app/conf/spring/resources.groovy file

import demo.Book
import org.grails.plugins.restawesome.renderer.ApiCollectionRendererJson
import org.grails.plugins.restawesome.renderer.ApiRendererJson

beans = {
    bookRenderer(ApiRendererJson, Book) {
        label = "book"
    }

    bookCollectionRenderer(ApiCollectionRendererJson, Book) {
        label = "books"
    }
}

label is optional. If you provide, it will use the provided label otherwise it has a fallback value which is "entity" for individual item and "entities" for collection.

After registering marshallers, you will have to extend "AwesomeRestfulController" provided by this plugin.

Our sample BookController should look like this.

package demo

import org.grails.plugins.restawesome.AwesomeRestfulController

class BookController extends AwesomeRestfulController {

    static responseFormats = ['json']

    BookController(){
        super(Book)
    }
}

What will you get?

Here is sample output of curl request to this plugin.

curl  http://localhost:8080/book
{"books":[
{"class":"demo.Book","id":1,"price":33.2,"title":"Grails in Action"},
{"class":"demo.Book","id":2,"price":20.2,"title":"Groovy in Action"}],
"paging":{"totalCount":2,"currentMax":10,"curentOffset":0}}

You can also sort this response with any of the attributes of Book class. To sort with price,

curl  http://localhost:8080/book?sort=price
{"books":[
{"class":"demo.Book","id":2,"price":20.2,"title":"Groovy in Action"},
{"class":"demo.Book","id":1,"price":33.2,"title":"Grails in Action"}],
"paging":{"totalCount":2,"currentMax":10,"curentOffset":0}}

We can also change the order of sort like "desc" or "asc".

Sorted by price in descending order:

curl  "http://localhost:8080/book?sort=price&order=desc"
{"books":[
{"class":"demo.Book","id":1,"price":33.2,"title":"Grails in Action"},
{"class":"demo.Book","id":2,"price":20.2,"title":"Groovy in Action"}],
"paging":{"totalCount":2,"currentMax":10,"curentOffset":0}}

Sorted by price in ascending order:

curl  "http://localhost:8080/book?sort=price&order=asc"
{"books":[
{"class":"demo.Book","id":2,"price":20.2,"title":"Groovy in Action"},
{"class":"demo.Book","id":1,"price":33.2,"title":"Grails in Action"}],
"paging":{"totalCount":2,"currentMax":10,"curentOffset":0}}

About SaaSMax

SaaSMAX is the growth engine for SaaS companies and their resellers. Our mission is all about recurring SaaS commissions.

About

This plugin add awesomeness to the default REST response of grails applications such as pagination, sorting and include/exclude fields

Resources

Stars

Watchers

Forks

Packages

No packages published