SlideShare a Scribd company logo
1 of 13
PRACTICAL REST IN GRAILS 2.3
dan woods
@danveloper
Introduction to Grails 2.3 REST

The Concept of a “Resource”
• Map data to a URL Endpoint
• Traverse object graph through URL mappings
• Provide serialization in your favorite format
• Driven by the HTTP Request’s Accept header
• Limitations:
• The whole URL graph needs to be explicitly defined for
mapping nested Resources
Introduction to Grails 2.3 REST

Defining a Resource
•
•
•
•

Decorate your domain object with the @Resource Annotation
Map to a URI via Annotation or UrlMapping
Use built-in, good-for-most-things RestfulController, or…
Build a custom controller to handle your specific use-case
Introduction to Grails 2.3 REST

Customizing Resource Behavior
•
•
•

Regulate response formats through the @Resource annotation
Determine read/write behavior
• In turn, writable actions will get 405
Customizing response
Introduction to Grails 2.3 REST

Resource Renderers
•
•

Provides inherent support for versioning
Built-in Renderers like DefaultJsonRenderer, HalJsonRenderer
offer some standard support
• include/exclude properties
• Handling versioning
Introduction to Grails 2.3 REST

Resource Renderers
•
•
•
•
•
•

Custom Renderers satisfy specific needs
Intercept the response activity to influence the output
Allow for changing the response structure
This is the place to put business logic that responds with a nonstandard view
Preferred over JSON.registerObjectMarshaller
Auto-wireable Spring beans
Introduction to Grails 2.3 REST

Resource Controllers
From the JAX-RS Spec…

"Adopting the MVC terminology, JAX-RS resource classes are
comparable to controllers"
https://jcp.org/en/jsr/detail?id=339

• Grails maps a RestfulController to the Resource’s URL, so the
@Resource is not the handler, it just represents the handler!
Beyond the Basics

Custom RestfulController
•
•
•
•

Overload how resources are resolved
Custom binding logic
Interface with services for object composition
• Business Logic: “Only show me healthy applications”
Using the respond method
• Control the response format this way
Getting Practical

Let’s get practical…
Getting Practical

Obscuring Ids
•
•
•

Custom RestfulController
• Overload queryForResource, listAllResources
Custom Renderer
• Only expose the properties that you want people to see
The @RestIdProperty strategy for building HAL Navigation
Getting Practical

Resources as a Concept
•

Nothing hard-ties a Resource to a domain object

•

Can be backed simply by a command object

•

Good choice for providing data in a format that is domainspecific, but brought in via service call
Getting Practical

Using Hyper-Media Practically
•

Built-in Grails rendering constructs don’t favor reusability

•

grails-rest-renderers plugin can provide some simplifications to
building pragmatic hyper-media renderers
http://grails.org/plugins/grails-rest-renderers
Getting Practical

Security?
Great article about REST Security from Dave Syer (@david_syer):
http://blog.cloudfoundry.com/2012/10/09/securing-restful-webservices-with-oauth2/
Summary:
Oauth: "one of the key reasons for OAuth2 to exist is so that
Client applications do not need to collect user credentials. Here is
where the learning curve for OAuth2 gets steeper.”
Form Login: Difficult to drive RESTful interactions since failures
are redirected to login page
Basic Auth: Maybe the best choice

More Related Content

More from Daniel Woods

Continuous Delivery with Spinnaker and OpenStack
Continuous Delivery with Spinnaker and OpenStackContinuous Delivery with Spinnaker and OpenStack
Continuous Delivery with Spinnaker and OpenStackDaniel Woods
 
Groovy in the Cloud
Groovy in the CloudGroovy in the Cloud
Groovy in the CloudDaniel Woods
 
Ratpack - SpringOne2GX 2015
Ratpack - SpringOne2GX 2015Ratpack - SpringOne2GX 2015
Ratpack - SpringOne2GX 2015Daniel Woods
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web FrameworkDaniel Woods
 
Microservices: The Right Way
Microservices: The Right WayMicroservices: The Right Way
Microservices: The Right WayDaniel Woods
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web FrameworkDaniel Woods
 
Facilitating Continuous Delivery at Scale
Facilitating Continuous Delivery at ScaleFacilitating Continuous Delivery at Scale
Facilitating Continuous Delivery at ScaleDaniel Woods
 
Continuous Delivery with NetflixOSS
Continuous Delivery with NetflixOSSContinuous Delivery with NetflixOSS
Continuous Delivery with NetflixOSSDaniel Woods
 
Server-Side JavaScript with Nashorn
Server-Side JavaScript with NashornServer-Side JavaScript with Nashorn
Server-Side JavaScript with NashornDaniel Woods
 
Groovy for System Administrators
Groovy for System AdministratorsGroovy for System Administrators
Groovy for System AdministratorsDaniel Woods
 
Message Driven Architecture in Grails
Message Driven Architecture in GrailsMessage Driven Architecture in Grails
Message Driven Architecture in GrailsDaniel Woods
 
Building Web Apps in Ratpack
Building Web Apps in RatpackBuilding Web Apps in Ratpack
Building Web Apps in RatpackDaniel Woods
 
Gainesville Web Developer Group, Sept 2012
Gainesville Web Developer Group, Sept 2012Gainesville Web Developer Group, Sept 2012
Gainesville Web Developer Group, Sept 2012Daniel Woods
 

More from Daniel Woods (14)

Continuous Delivery with Spinnaker and OpenStack
Continuous Delivery with Spinnaker and OpenStackContinuous Delivery with Spinnaker and OpenStack
Continuous Delivery with Spinnaker and OpenStack
 
Groovy in the Cloud
Groovy in the CloudGroovy in the Cloud
Groovy in the Cloud
 
Ratpack - SpringOne2GX 2015
Ratpack - SpringOne2GX 2015Ratpack - SpringOne2GX 2015
Ratpack - SpringOne2GX 2015
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web Framework
 
Microservices: The Right Way
Microservices: The Right WayMicroservices: The Right Way
Microservices: The Right Way
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web Framework
 
Facilitating Continuous Delivery at Scale
Facilitating Continuous Delivery at ScaleFacilitating Continuous Delivery at Scale
Facilitating Continuous Delivery at Scale
 
Continuous Delivery with NetflixOSS
Continuous Delivery with NetflixOSSContinuous Delivery with NetflixOSS
Continuous Delivery with NetflixOSS
 
Server-Side JavaScript with Nashorn
Server-Side JavaScript with NashornServer-Side JavaScript with Nashorn
Server-Side JavaScript with Nashorn
 
Future of Grails
Future of GrailsFuture of Grails
Future of Grails
 
Groovy for System Administrators
Groovy for System AdministratorsGroovy for System Administrators
Groovy for System Administrators
 
Message Driven Architecture in Grails
Message Driven Architecture in GrailsMessage Driven Architecture in Grails
Message Driven Architecture in Grails
 
Building Web Apps in Ratpack
Building Web Apps in RatpackBuilding Web Apps in Ratpack
Building Web Apps in Ratpack
 
Gainesville Web Developer Group, Sept 2012
Gainesville Web Developer Group, Sept 2012Gainesville Web Developer Group, Sept 2012
Gainesville Web Developer Group, Sept 2012
 

Recently uploaded

Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...FIDO Alliance
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfFIDO Alliance
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfFIDO Alliance
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...FIDO Alliance
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Hiroshi SHIBATA
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024Stephen Perrenod
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...CzechDreamin
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsStefano
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessUXDXConf
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfUK Journal
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 

Recently uploaded (20)

Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 

Practical REST in Grails 2.3

  • 1. PRACTICAL REST IN GRAILS 2.3 dan woods @danveloper
  • 2. Introduction to Grails 2.3 REST The Concept of a “Resource” • Map data to a URL Endpoint • Traverse object graph through URL mappings • Provide serialization in your favorite format • Driven by the HTTP Request’s Accept header • Limitations: • The whole URL graph needs to be explicitly defined for mapping nested Resources
  • 3. Introduction to Grails 2.3 REST Defining a Resource • • • • Decorate your domain object with the @Resource Annotation Map to a URI via Annotation or UrlMapping Use built-in, good-for-most-things RestfulController, or… Build a custom controller to handle your specific use-case
  • 4. Introduction to Grails 2.3 REST Customizing Resource Behavior • • • Regulate response formats through the @Resource annotation Determine read/write behavior • In turn, writable actions will get 405 Customizing response
  • 5. Introduction to Grails 2.3 REST Resource Renderers • • Provides inherent support for versioning Built-in Renderers like DefaultJsonRenderer, HalJsonRenderer offer some standard support • include/exclude properties • Handling versioning
  • 6. Introduction to Grails 2.3 REST Resource Renderers • • • • • • Custom Renderers satisfy specific needs Intercept the response activity to influence the output Allow for changing the response structure This is the place to put business logic that responds with a nonstandard view Preferred over JSON.registerObjectMarshaller Auto-wireable Spring beans
  • 7. Introduction to Grails 2.3 REST Resource Controllers From the JAX-RS Spec… "Adopting the MVC terminology, JAX-RS resource classes are comparable to controllers" https://jcp.org/en/jsr/detail?id=339 • Grails maps a RestfulController to the Resource’s URL, so the @Resource is not the handler, it just represents the handler!
  • 8. Beyond the Basics Custom RestfulController • • • • Overload how resources are resolved Custom binding logic Interface with services for object composition • Business Logic: “Only show me healthy applications” Using the respond method • Control the response format this way
  • 10. Getting Practical Obscuring Ids • • • Custom RestfulController • Overload queryForResource, listAllResources Custom Renderer • Only expose the properties that you want people to see The @RestIdProperty strategy for building HAL Navigation
  • 11. Getting Practical Resources as a Concept • Nothing hard-ties a Resource to a domain object • Can be backed simply by a command object • Good choice for providing data in a format that is domainspecific, but brought in via service call
  • 12. Getting Practical Using Hyper-Media Practically • Built-in Grails rendering constructs don’t favor reusability • grails-rest-renderers plugin can provide some simplifications to building pragmatic hyper-media renderers http://grails.org/plugins/grails-rest-renderers
  • 13. Getting Practical Security? Great article about REST Security from Dave Syer (@david_syer): http://blog.cloudfoundry.com/2012/10/09/securing-restful-webservices-with-oauth2/ Summary: Oauth: "one of the key reasons for OAuth2 to exist is so that Client applications do not need to collect user credentials. Here is where the learning curve for OAuth2 gets steeper.” Form Login: Difficult to drive RESTful interactions since failures are redirected to login page Basic Auth: Maybe the best choice

Editor's Notes

  1. show default url-mappings-reportshow querying the graph
  2. Show @Resource(formats = …)Show url-mappings-report with UrlMapping and w/ uri defined on Resource
  3. > Demonstrate include/exclude properties on DefaultJsonRendererDemonstrate Application HalJsonRendererapplicationRenderer(JsonRenderer, Application, new MimeType("application/json", [v: "1.0"]))Our persistence model (ie our domain model) doesn’t always make sense as a View Model, so for that we need some finer-grained control
  4. > Demonstrate include/exclude properties on DefaultJsonRenderer> Demonstrate Application HalJsonRendererOur persistence model (ie our domain objects) doesn’t always make sense as a View Model, so for that we need some finer-grained controlShow Android app view model consumer example.
  5. curl -X PUT -H "Content-type: application/json" -d "{ \"healthy\": false }" http://localhost:8080/practical-rest/rest/applications/1
  6. curl -X PUT -H "Content-type: application/json" -d "{ \"healthy\": false }" http://localhost:8080/practical-rest/rest/applications/1
  7. curl http://localhost:8080/eidyia/api/repository?org=netflix
  8. curl -H "Accept: application/json" http://localhost:8080/practical-rest/rest/applications/abcassandra/autoScalingGroups
  9. This is a topic all in itself…