Restful Grails3

19
RESTful Grails 3 Jeff Scott Brown @jeffscottbrown [email protected]

Transcript of Restful Grails3

Page 1: Restful Grails3

RESTful Grails 3

Jeff Scott Brown @jeffscottbrown

[email protected]

Page 2: Restful Grails3

More at ociweb.com/grails

Page 3: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

WE ARE HIRING!

Groovy And Grails Project Work Grails 2 -> 3 Plugin Migrations Grails Plugin Development Expanding GORM’s Reach New Application Profiles Grails Core Development

[email protected]

Page 4: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

“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.”

4

REST

— http://en.wikipedia.org/wiki/Representational_state_transfer

Page 5: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Grails And REST

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

• Customization Options • Renderers • Data Binding

5

Page 6: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Standard Domain Class

6

Page 7: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

URL Mapping

7

Page 8: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

URL Mapping

8

Method URI ActionGET /people indexGET /people/create createPOST /people saveGET /people/{id} showGET /people/{id}/edit editPUT /people/{id} updateDELETE /people/{id} delete

Page 9: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

REST Requests

9

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 10: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Controller Action Allowed Methods

10

Don’t Need To Do This If Using allowedMethods.

Page 11: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Data Binding And Command Objects

11

Page 12: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Data Binding And Domain Class Command Objects

12

Page 13: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Domain Class Resource Annotation

13

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

Page 14: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Responding

14

Preferred

Page 15: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Customizing The Default Renderers

15

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 16: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Implementing A Custom Renderer

16

Page 17: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Registering A Customer Renderer

17

curl -i http://localhost:8080/restconfdemo/people/2.xml

<person yearsOld='16' first='Zack' last='Brown' />

Page 18: Restful Grails3

Copyright (c) 2015 Object Computing, Inc. All rights reserved.

Extending RestfulController

18

Page 19: Restful Grails3

Q&A

Jeff Scott Brown @jeffscottbrown

[email protected]