Skip to content

Instantly share code, notes, and snippets.

@novotnyr
Created August 15, 2013 10:22
Show Gist options
  • Save novotnyr/6239833 to your computer and use it in GitHub Desktop.
Save novotnyr/6239833 to your computer and use it in GitHub Desktop.
@Grab(group='org.codehaus.jackson', module='jackson-mapper-asl', version='1.9.13')
@Controller
@RequestMapping("/chocolates")
class ChocolateController {
def chocolates = [
[ id: 1, name: "Lindt", percentage: "70%" ],
[ id: 2, name: "Liedl", percentage: "15%" ]
]
@RequestMapping
@ResponseBody
def getAll() {
chocolates
}
@RequestMapping("/{id}")
@ResponseBody
def get(@PathVariable long id) {
def chocolate = chocolates.find { it.id == id }
if(chocolate == null) {
throw new ResourceNotFoundException(resourceId: id)
}
chocolate
}
@ExceptionHandler(ResourceNotFoundException)
@ResponseBody
def onResourceNotFound(def exception) {
[ code : 404, id : exception.resourceId ]
}
}
@ResponseStatus(value=HttpStatus.NOT_FOUND)
class ResourceNotFoundException extends RuntimeException {
def resourceId
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment