Restful Grails3

Post on 08-Jan-2017

948 views 0 download

Transcript of Restful Grails3

RESTful Grails 3

Jeff Scott Brown @jeffscottbrown

brownj@ociweb.com

More at ociweb.com/grails

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

grailsjobs@ociweb.com

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

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

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

Standard Domain Class

6

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

URL Mapping

7

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

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

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

Controller Action Allowed Methods

10

Don’t Need To Do This If Using allowedMethods.

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

Data Binding And Command Objects

11

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

Data Binding And Domain Class Command Objects

12

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…

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

Responding

14

Preferred

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

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

Implementing A Custom Renderer

16

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' />

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

Extending RestfulController

18

Q&A

Jeff Scott Brown @jeffscottbrown

brownj@ociweb.com