Giter Site home page Giter Site logo

ejaz-ahmed / grails-rest-awesome-plugin Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 2.0 202 KB

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

JavaScript 2.51% CSS 65.50% Groovy 31.99%

grails-rest-awesome-plugin's Introduction

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.

grails-rest-awesome-plugin's People

Contributors

burtbeckwith avatar ejaz-ahmed avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

burtbeckwith

grails-rest-awesome-plugin's Issues

Modify response

How can i modify the output. For example i have a foreign key of Author domain name in Book domain. How do i get the property name is Author to show in the Book response from the api

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.