Tuesday, July 12, 2016

Control Pagination of Restful Controller

Problem: The default index action (scaffolded) paginate the result, even for xml and json request *that may not be needed for many cases and the result doesn't indicate it is paginated)

Solution: Using withFormat (response not request) and indicating each format (including html) individually. Using '*' overwrite all formats and results in unexpected format.

params.max = Math.min(max ?: 10, 100)
withFormat {
    'html' {
        respond UnknownNode.list(params), model: [unknownNodeInstanceCount: UnknownNode.count()]
    }
    'json' { respond UnknownNode.list(), [status: CREATED] }
    'xml' { respond UnknownNode.list(), [status: CREATED] }
}