Last active
August 29, 2015 13:56
-
-
Save spikeheap/9213738 to your computer and use it in GitHub Desktop.
Using Grails 'respond' for HTML, JSON and XML responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show(Widget widget){ | |
| // renders a blank page for null objects | |
| respond widget, [model: [widget: widget]] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show(Widget widget){ | |
| if(!widget){ | |
| // Pretty 404 page displayed, even for JSON requests | |
| response.sendError(404) | |
| }else{ | |
| // Responds normally for non-empty, non-null objects | |
| respond widget, [model: [widget: widget]] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show(Widget widget){ | |
| if(!widget){ | |
| // 404 status for all, pretty 404 page for users | |
| respond null, [status: 404, view: 'error404'] | |
| }else{ | |
| respond widget, [model: [widget: widget]] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show(Widget widget){ | |
| if(!widget){ | |
| def model = [ success: false, msg: [ code: 404, text: "The widget could not be found"]] | |
| // 404 status for all, JSON/XML/HTML appropriate response with detail | |
| respond model as Object, [model: model, status: 404, view: 'error404'] | |
| }else{ | |
| respond widget, [model: [widget: widget]] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment