RESTful Groovy

5,411 views

Published on

Speaker: Kenneth Kousen
The JAX-RS 2.0 specification is part of Java EE 7, but can be used now. It contains the expected annotations for the HTTP verbs (@GET, @POST, and so on) and mechanisms for retrieving variables, but only a few methods for doing hypermedia. This presentation will review those techniques by adding both structural and transitional links to resource representations. Groovy is used to simplify the code and also to implement a MessageBodyWriter for JSON data.
In addition to simplifying the implementation classes, Groovy also supplies a RESTful client class from the HttpBuilder project. That will be used, as well as the native client implementation classes in JAX-RS 2.0, to build Spock tests for RESTful services.

Published in: Technology

RESTful Groovy

  1. 1. RESTful Groovy Implementing JAX-RS with Groovy
  2. 2. Contact Info Ken Kousen ken.kousen@kousenit.com @kenkousen Making Java Groovy http://manning.com/kousen
  3. 3. REST Term from Ph.D. thesis by Roy Fielding Co-founder Apache HTTP Server project Part of IETF working groups for: URI, HTTP, HTML Architectural Styles and the Design of Network-based Software Architectures
  4. 4. REST Representational State Transfer - Addressable resources - Uniform interface - Content negotiation - Stateless services
  5. 5. Addressable Resources Use URLs to access items (no verbs in URLs)
  6. 6. Uniform Interface HTTP verbs GET POST PUT DELETE ---HEAD OPTIONS PATCH
  7. 7. Safe vs Idempotent Safe → no server side changes GET
  8. 8. Idempotent Idempotent requests can be repeated No additional effects GET PUT DELETE
  9. 9. Http Status Codes 200s → good 200 OK, 201 created, 204 no content 300s → moved 400s → you messed up 404 not found, 409 conflict (see 418, 420 for fun) 500s → we messed up
  10. 10. RESTful service Implementations GET → retrieve data POST → insert data set Location header for new item PUT → update data DELETE → remove data
  11. 11. RESTful service Yes, that’s a URL-driven database
  12. 12. RESTful service Yes, that’s a URL-driven database (Hypermedia advocates are squirming, but we’ll get there)
  13. 13. Content Negotiation Client can request representations Usually via MIME type in Accept header
  14. 14. Stateless Services No state maintained in service All interactions via self-contained requests
  15. 15. HATEOAS Hypermedia As The Engine Of Application State An unpronounceable acronym with the word HATE in it
  16. 16. Hypermedia Goal is true decoupling Client asks for first URL Response includes links Works like the web, but programmatically
  17. 17. The Java Approach JAX-RS 1.1 Implements JSR 311 spec JAX-RS 2.0 Implements JSR 339 spec
  18. 18. JAX-RS Implementations Jersey (R.I.) RestEasy Restlets Restfulie Apache CXF
  19. 19. JAX-RS Note: Spring REST annotations do NOT implement JAX-RS
  20. 20. POGO Resources are POGOs Representations are based on MIME-types Hypermedia comes from links and annotations
  21. 21. Server Side
  22. 22. JAX-RS Annotations @GET, @POST, @PUT, @DELETE @Produces, @Consumes @Path, @PathParam @Context
  23. 23. JAX-RS Classes, interfaces, enums Response Response.ResponseBuilder UriInfo UriBuilder MediaType
  24. 24. Client Side HttpBuilder project wraps Apache HTTP Client http://groovy.codehaus.org/modules/http-builder/ Source code: https://github.com/jgritman/httpbuilder
  25. 25. Client Side JAX-RS 2.0 includes client library ClientBuilder WebTarget
  26. 26. Hypermedia
  27. 27. Hypermedia
  28. 28. Trivia Did you know? Neither the word “hypermedia” nor HATEOAS appear anywhere in the JSR 399 specification (No, I don’t know why either)
  29. 29. Links Adding transitional links Response.ResponseBuilder link (several overloads) links Link class
  30. 30. Links Adding structural links Use Link attributes inside POGO Add JAXB annotations @XmlJavaTypeAdapter(JaxbAdapter)
  31. 31. Links Customizing links Groovy API: JsonBuilder JsonSlurper Use a custom JAX-RS provider
  32. 32. Custom Provider Implement: MessageBodyReader<T> MessageBodyWriter<T>
  33. 33. Summary JAX-RS 2.0 specification Jersey reference implementation Hypermedia done through links Groovy simplifies code No major changes unless custom provider HttpBuilder client

×