Your SlideShare is downloading. ×
0
Building APIs with Grails
SpringOne2GX
September 12th, 2013
Santa Clara, CA
Bobby Warner
Who Am I
•
•

Blog

•

http://bobbywarner.com

Social Networks

•
•
•
•

@bobbywarner
https://www.facebook.com/bobbywarner...
Where I Used To Work
My New Company
Groovy Users of Minnesota (GUM)
Question #1

• What is a modern API?
Purpose
•
•
•
•

JavaScript clients (Backbone.js, Angular.js, Ember.js, etc.)
Mobile clients (Android, iOS, etc.)
Service ...
Architecture
•
•
•
•
•

Client/Server
Stateless
Caching
Uniform Interface
Layered System
Principles
•
•
•
•

Endpoints
Resources (hierarchy, namespaces, representation)
Methods (GET, POST, PUT, DELETE)
Represent...
Status Codes: Success
•

Fetching

•

200: ok

•

204: no content

•

206: partial content

•

Changing

•

201: created

...
Status Codes: Failures
•

Client Failures:

•
•

4xx

Server Failures:

•

5xx
Versioning
•
•
•

URI component (http://myapi.com/v1/books)
Query parameter (http://myapi.com/books?version=v1)
Header (ht...
System to System:
Authentication
•
•
•
•

Basic
Digest
API Keys
Request Signing
User to System:
OAuth Resource Server
•
•

Gateway products (i.e. Apigee, Layer 7)
OAuth provider libraries (i.e. Spring S...
Question #2

• What are the different ways we can build APIs?
Groovy API Options
•

JAX-RS (Jersey, RESTEasy, Apache CXF, Restlet, etc.)

•
•
•
•
•

Grails JAX-RS Plugin

Spring (RestT...
Question #3

• Why should we use Grails to build APIs?
Grails 2.2 and below
•

http://www.bobbywarner.com/2013/04/18/building-apis-with-grails/

•
•

ObjectMarshaller
GSON Plugi...
Grails 2.3
•
•
•

Grails has always supported building APIs, but now it’s a lot better
There have been many different plug...
Benefits of Using Grails 2.3
•
•
•
•

Support for resource mappings, nested resources and versioning
Extensible response re...
Question #4

• How do I build an API with Grails 2.3?
Grails 2.3 APIs
•
•
•

Option #1: The resource annotation on domains
Option #2: Extend RestfulController and override meth...
Option #1: Resource Annotation

•
•

grails create-domain-class book
@Resource(uri='/books', formats=['json', 'xml'])
Option #1: Resource Annotation

• Demo!
Option #2: Extend RestfulController
Option #2: Extend RestfulController

• Demo!
Option #3: Start From Scratch
Option #3: Start From Scratch
•

The key to implementing REST actions is the respond method
introduced in Grails 2.3.

•

...
Option #3: Start From Scratch

• Demo!
URL Mappings

•

grails url-mapping-report
Question #5

• How do I customize the XML or JSON response?
Customize the Response
•
•
•
•

Option #1: Default Renderers (Include or Exclude specific properties)
Option #2: Custom Ren...
Option #1: Default Renderers
Option #2: Custom Renderers
Option #3: Converters
Option #4: GSPs
Question #6

• How do I version a Grails API?
Versioning
•

Option #1: URI

•

Option #2: Accept Header ("Accept-Version: 1.0")
Versioning
Hypermedia
•
•
•
•

Hypermedia as the Engine of Application State (HATEOS)
Independent evolution
Decoupled Implementation
...
Hypermedia in Grails
•
•
•

HAL Support (standard exchange format)
Versioning via MIME Types
HAL Renderers (HalJsonRendere...
Thoughts on Hypermedia APIs
•

Theoretically, you will be able to change your URLs without needing
to update clients, but ...
Thoughts on Hypermedia APIs
•
•

Very interesting in concept, but still not convinced it’s practical
Don’t worry so much a...
What’s Next
•
•
•

Testing, finding / fixing bugs
Documentation updates (need more examples)
New screen-cast in the works de...
Closing
•
•

Thank you for attending this presentation!

•

Hopefully you use Grails 2.3 to create some APIs!

•

Addition...
Q &A

•

Are there any questions?
Upcoming SlideShare
Loading in...5
×

Building APIs with Grails

2,443

Published on

Speaker: Bobby Warner
APIs open up our application data and functionality to other clients like mobile applications, JavaScript clients and even potentially other companies. The most prominent way of implementing them is by using the REST architectural style and it is now almost expected for all web developers to know how to build good RESTful APIs. As Grails developers, we have a few options readily available to us to simplify this task and Grails 2.3 included lots of new REST support.
This talk will cover these options as well as discuss some of the pros and cons with each approach so you can successfully build APIs using the new REST features in Grails 2.3 for your next project.

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,443
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
50
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Building APIs with Grails"

  1. 1. Building APIs with Grails SpringOne2GX September 12th, 2013 Santa Clara, CA Bobby Warner
  2. 2. Who Am I • • Blog • http://bobbywarner.com Social Networks • • • • @bobbywarner https://www.facebook.com/bobbywarner http://www.linkedin.com/in/bobbywarner http://bobbywarner.com/googleplus
  3. 3. Where I Used To Work
  4. 4. My New Company
  5. 5. Groovy Users of Minnesota (GUM)
  6. 6. Question #1 • What is a modern API?
  7. 7. Purpose • • • • JavaScript clients (Backbone.js, Angular.js, Ember.js, etc.) Mobile clients (Android, iOS, etc.) Service Oriented Architecture Sharing data with other companies
  8. 8. Architecture • • • • • Client/Server Stateless Caching Uniform Interface Layered System
  9. 9. Principles • • • • Endpoints Resources (hierarchy, namespaces, representation) Methods (GET, POST, PUT, DELETE) Representations (JSON, XML)
  10. 10. Status Codes: Success • Fetching • 200: ok • 204: no content • 206: partial content • Changing • 201: created • 202: accepted (later processing) • 204: no content (PUT or DELETE succeeded) • GET returns 200, 204, 206 • HEAD returns 204 • POST returns 201, 202 • PUT returns 201, 202, 204 • DELETE returns 202, 204
  11. 11. Status Codes: Failures • Client Failures: • • 4xx Server Failures: • 5xx
  12. 12. Versioning • • • URI component (http://myapi.com/v1/books) Query parameter (http://myapi.com/books?version=v1) Header (http://myapi.com/books + HTTP header)
  13. 13. System to System: Authentication • • • • Basic Digest API Keys Request Signing
  14. 14. User to System: OAuth Resource Server • • Gateway products (i.e. Apigee, Layer 7) OAuth provider libraries (i.e. Spring Security OAuth)
  15. 15. Question #2 • What are the different ways we can build APIs?
  16. 16. Groovy API Options • JAX-RS (Jersey, RESTEasy, Apache CXF, Restlet, etc.) • • • • • Grails JAX-RS Plugin Spring (RestTemplate) Dropwizard (uses Jersey) Groovy directly w/o frameworks (JsonBuilder) ...and of course Grails!
  17. 17. Question #3 • Why should we use Grails to build APIs?
  18. 18. Grails 2.2 and below • http://www.bobbywarner.com/2013/04/18/building-apis-with-grails/ • • ObjectMarshaller GSON Plugin
  19. 19. Grails 2.3 • • • Grails has always supported building APIs, but now it’s a lot better There have been many different plugins filling in the gaps Significant REST enhancements included in Grails 2.3
  20. 20. Benefits of Using Grails 2.3 • • • • Support for resource mappings, nested resources and versioning Extensible response rendering and binding APIs Scaffolding for REST controllers Support for HAL, Atom and Hypermedia (HATEAOS)
  21. 21. Question #4 • How do I build an API with Grails 2.3?
  22. 22. Grails 2.3 APIs • • • Option #1: The resource annotation on domains Option #2: Extend RestfulController and override methods Option #3: Start from scratch
  23. 23. Option #1: Resource Annotation • • grails create-domain-class book @Resource(uri='/books', formats=['json', 'xml'])
  24. 24. Option #1: Resource Annotation • Demo!
  25. 25. Option #2: Extend RestfulController
  26. 26. Option #2: Extend RestfulController • Demo!
  27. 27. Option #3: Start From Scratch
  28. 28. Option #3: Start From Scratch • The key to implementing REST actions is the respond method introduced in Grails 2.3. • Respond method tries to produce the most appropriate response for the requested content type (JSON, XML, HTML etc.)
  29. 29. Option #3: Start From Scratch • Demo!
  30. 30. URL Mappings • grails url-mapping-report
  31. 31. Question #5 • How do I customize the XML or JSON response?
  32. 32. Customize the Response • • • • Option #1: Default Renderers (Include or Exclude specific properties) Option #2: Custom Renderers Option #3: Converters (Object Marshallers) Option #4: GSPs
  33. 33. Option #1: Default Renderers
  34. 34. Option #2: Custom Renderers
  35. 35. Option #3: Converters
  36. 36. Option #4: GSPs
  37. 37. Question #6 • How do I version a Grails API?
  38. 38. Versioning • Option #1: URI • Option #2: Accept Header ("Accept-Version: 1.0")
  39. 39. Versioning
  40. 40. Hypermedia • • • • Hypermedia as the Engine of Application State (HATEOS) Independent evolution Decoupled Implementation Pick a hypermedia-aware data type (raw XML and JSON aren’t)
  41. 41. Hypermedia in Grails • • • HAL Support (standard exchange format) Versioning via MIME Types HAL Renderers (HalJsonRenderer or HalXmlRenderer)
  42. 42. Thoughts on Hypermedia APIs • Theoretically, you will be able to change your URLs without needing to update clients, but this isn’t practical • • Not every API client will come through the front door Most developers would rather look at docs as opposed to making many GET requests to understand the API
  43. 43. Thoughts on Hypermedia APIs • • Very interesting in concept, but still not convinced it’s practical Don’t worry so much about HATEOS and instead focus on these three things: • • • Don’t change your API URLs Document your API Provide a custom client wrapper
  44. 44. What’s Next • • • Testing, finding / fixing bugs Documentation updates (need more examples) New screen-cast in the works demoing more 2.3 REST features!
  45. 45. Closing • • Thank you for attending this presentation! • Hopefully you use Grails 2.3 to create some APIs! • Additional Resources Thank you SpringOne2GX organizers for hosting a great conference! • http://grails.org/doc/latest/guide/single.html#REST
  46. 46. Q &A • Are there any questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×