Create an Exception class like com.my.ResourceNotFoundException and then throw it from whatever place you want (controller or service).
Create a controller like the one below:
class ErrorController {
def error404() {
response.status = HttpStatus.NOT_FOUND.value()
render([errors: [[message: "The resource you are looking for could not be found"]]] as JSON)
}
}
Then add into your UrlMappings.groovy configuration file an entry that will handle exceptions of that type with this controller action. Specifying "500" as the pattern means that it will catch 500 errors (like the one your thrown ResourceNotFoundException will cause) and if the exception matches that type it will use the controller and action specified.
"500"(controller: "error", action: "error404", exception: ResourceNotFoundException)