In Grails we can apply the @Resource AST (Abstract Syntax Tree) annotation to a domain class. Grails will generate a complete new controller which by default extends grails.rest.RestfulController. We can use our own controller class that will be extended by the @Resource transformation. For example we might want to disable the delete action, but still want to use the @Resource transformation. We simply write a new RestfulController implementation and use the superClass attribute for the annotation to assign our custom controller as the value.
First we write a new RestfulController and we override the delete action. We return a HTTP status code 405 Method not allowed:
// File: grails-app/controllers/com/mrhaki/grails/NonDeleteRestfulController.groovy
package com.mrhaki.grails
import grails.rest.*
import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED
/**
* Custom RestfulController where we disable the delete action.
*/
class NonDeleteRestfulController<T> extends RestfulController<T> {
// We need to provide the constructors, so the
// Resource transformation works.
NonDeleteRestfulController(Class<T> domainClass) {
this(domainClass, false)
}
NonDeleteRestfulController(Class<T> domainClass, boolean readOnly) {
super(domainClass, readOnly)
}
@Override
def delete() {
// Return Method not allowed HTTP status code.
render status: METHOD_NOT_ALLOWED
}
}
Next we indicate in the @Resource annotation with the superClass attribute that we want to use the NonDeleteRestfulController:
// File: grails-app/domain/com/mrhaki/grails/User.groovy
package com.mrhaki.grails
import grails.rest.*
@Resource(uri = '/users', superClass = NonDeleteRestfulController)
class User {
String username
String lastname
String firstname
String email
static constraints = {
email email: true
lastname nullable: true
firstname nullable: true
}
}
When we access the resource /users/{id} with the HTTP DELETE method we get a 405 Method Not Allowed response status code.
Written with Grails 2.4.2.