© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
RESTful Grails 2Jeff Scott Brown @jeffscottbrown
“REST is an architectural style consisting of a coordinated set of architectural constraints applied to components, connectors, and data elements, within a distributed hypermedia system.”
2
REST
— http://en.wikipedia.org/wiki/Representational_state_transfer
Grails And REST
• Out Of The Box Support For Common Cases • URL Mappings • Data Binding • Domain Class Resources
• Customization Options • Renderers • Data Binding
3
Standard Domain Class
4
URL Mapping
5
URL Mapping
6
Method URI ActionGET /people indexGET /people/create createPOST /people saveGET /people/{id} showGET /people/{id}/edit editPUT /people/{id} updateDELETE /people/{id} delete
REST Requests
7
Get People: curl -H "Accept: application/json" ! -i http://localhost:8080/restconfdemo/people!!Create Person: curl -i -X POST -H "Content-Type: application/json" ! -H "Accept: application/json" ! -d '{"firstName":"Jake","lastName":"Brown","age":14}'! http://localhost:8080/restconfdemo/people !!Update Person: curl -i -X PUT -H "Content-Type: application/json" ! -H "Accept: application/json" ! -d '{“firstName":"Jacob"}'! http://localhost:8080/restconfdemo/people/1
Controller Action Allowed Methods
8
Don’t Need To Do This If Using allowedMethods.
Data Binding And Command Objects
9
Data Binding And Domain Class Command Objects
10
Domain Class Resource Annotation
11
• Same URL Mappings • Same Controller Behavior • Same Data Binding • Same Rendering • Etc…
Responding
12
Preferred
Customizing The Default Renderers
13
curl -i http://localhost:8080/restconfdemo/people.json ![{“class":"demo.Person","id":1,"firstName":"Jake","lastName":"Brown"}, {“class":"demo.Person","id":2,"firstName":"Zack","lastName":"Brown"}, {"class":"demo.Person","id":3,"firstName":"Jeff","lastName":"Brown"}]
Implementing A Custom Renderer
14
Registering A Customer Renderer
15
curl -i http://localhost:8080/restconfdemo/people/2.xml !<person yearsOld='16' first='Zack' last='Brown' />
Extending RestfulController
16
Q & A
17