+ All Categories
Home > Software > RESTful Grails 2

RESTful Grails 2

Date post: 02-Jul-2015
Category:
Upload: spring-io
View: 463 times
Download: 0 times
Share this document with a friend
Description:
Grails 2 includes a lot of features and functionality related to building RESTful services. These include an entirely new and more flexible data binding system, runtime and compile time metaprogramming which greatly reduce the amount of code required in your RESTful services, a rich set of content negotiation tools and more.
17
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission. RESTful Grails 2 Jeff Scott Brown @jeffscottbrown [email protected]
Transcript
Page 1: RESTful Grails 2

© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.

RESTful Grails 2Jeff Scott Brown @jeffscottbrown

[email protected]

Page 2: RESTful Grails 2

“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

Page 3: RESTful Grails 2

Grails And REST

• Out Of The Box Support For Common Cases • URL Mappings • Data Binding • Domain Class Resources

• Customization Options • Renderers • Data Binding

3

Page 4: RESTful Grails 2

Standard Domain Class

4

Page 5: RESTful Grails 2

URL Mapping

5

Page 6: RESTful Grails 2

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

Page 7: RESTful Grails 2

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

Page 8: RESTful Grails 2

Controller Action Allowed Methods

8

Don’t Need To Do This If Using allowedMethods.

Page 9: RESTful Grails 2

Data Binding And Command Objects

9

Page 10: RESTful Grails 2

Data Binding And Domain Class Command Objects

10

Page 11: RESTful Grails 2

Domain Class Resource Annotation

11

• Same URL Mappings • Same Controller Behavior • Same Data Binding • Same Rendering • Etc…

Page 12: RESTful Grails 2

Responding

12

Preferred

Page 13: RESTful Grails 2

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"}]

Page 14: RESTful Grails 2

Implementing A Custom Renderer

14

Page 15: RESTful Grails 2

Registering A Customer Renderer

15

curl -i http://localhost:8080/restconfdemo/people/2.xml !<person yearsOld='16' first='Zack' last='Brown' />

Page 16: RESTful Grails 2

Extending RestfulController

16

Page 17: RESTful Grails 2

Q & A

17


Recommended