Download - RESTful Groovy

Transcript
Page 1: RESTful Groovy

RESTful GroovyImplementing JAX-RS with Groovy

Page 3: RESTful Groovy

REST

Term from Ph.D. thesis by Roy FieldingCo-founder Apache HTTP Server projectPart of IETF working groups for:

URI, HTTP, HTML

Architectural Styles and the Design ofNetwork-based Software Architectures

Page 4: RESTful Groovy

REST

Representational State Transfer

- Addressable resources- Uniform interface- Content negotiation- Stateless services

Page 5: RESTful Groovy

Addressable Resources

Use URLs to access items(no verbs in URLs)

Page 6: RESTful Groovy

Uniform Interface

HTTP verbsGET

POST

PUT

DELETE

---- HEAD

OPTIONS

PATCH

Page 7: RESTful Groovy

Safe vs Idempotent

Safe → no server side changesGET

Page 8: RESTful Groovy

Idempotent

Idempotent requests can be repeatedNo additional effects

GETPUTDELETE

Page 9: RESTful Groovy

Http Status Codes

200s → good200 OK, 201 created, 204 no content

300s → moved400s → you messed up

404 not found, 409 conflict(see 418, 420 for fun)

500s → we messed up

Page 10: RESTful Groovy

RESTful service

ImplementationsGET → retrieve dataPOST → insert data

set Location header for new itemPUT → update dataDELETE → remove data

Page 11: RESTful Groovy

RESTful service

Yes, that’s a URL-driven database

Page 12: RESTful Groovy

RESTful service

Yes, that’s a URL-driven database

(Hypermedia advocates are squirming,but we’ll get there)

Page 13: RESTful Groovy

Content Negotiation

Client can request representations

Usually via MIME typein Accept header

Page 14: RESTful Groovy

Stateless Services

No state maintained in service

All interactions via self-contained requests

Page 15: RESTful Groovy

HATEOAS

HypermediaAsTheEngineOfApplicationState

An unpronounceable acronymwith the word HATE in it

Page 16: RESTful Groovy

Hypermedia

Goal is true decouplingClient asks for first URLResponse includes links

Works like the web, but programmatically

Page 17: RESTful Groovy

The Java Approach

JAX-RS 1.1Implements JSR 311 spec

JAX-RS 2.0Implements JSR 339 spec

Page 18: RESTful Groovy

JAX-RS

ImplementationsJersey (R.I.)RestEasyRestletsRestfulieApache CXF

Page 19: RESTful Groovy

JAX-RS

Note: Spring REST annotationsdo NOT implement JAX-RS

Page 20: RESTful Groovy

POGO

Resources are POGOs

Representations are based onMIME-types

Hypermedia comes from links and annotations

Page 21: RESTful Groovy

Server Side

Page 22: RESTful Groovy

JAX-RS

Annotations@GET, @POST, @PUT, @DELETE

@Produces, @Consumes

@Path, @PathParam

@Context

Page 23: RESTful Groovy

JAX-RS

Classes, interfaces, enumsResponse

Response.ResponseBuilder

UriInfo

UriBuilder

MediaType

Page 24: RESTful Groovy
Page 25: RESTful Groovy

Client Side

HttpBuilder projectwraps Apache HTTP Clienthttp://groovy.codehaus.org/modules/http-builder/

Source code:https://github.com/jgritman/httpbuilder

Page 26: RESTful Groovy

Client Side

JAX-RS 2.0 includes client libraryClientBuilder

WebTarget

Page 27: RESTful Groovy

Hypermedia

Page 28: RESTful Groovy

Hypermedia

Page 29: RESTful Groovy

Trivia

Did you know?

Neither the word “hypermedia” nor HATEOASappear anywhere in the JSR 399 specification

(No, I don’t know why either)

Page 30: RESTful Groovy

Links

Adding transitional linksResponse.ResponseBuilder

link (several overloads)links

Link class

Page 31: RESTful Groovy

Links

Adding structural linksUse Link attributes inside POGOAdd JAXB annotations

@XmlJavaTypeAdapter(JaxbAdapter)

Page 32: RESTful Groovy

Links

Customizing links

Groovy API: JsonBuilder

JsonSlurper

Use a custom JAX-RS provider

Page 33: RESTful Groovy

Custom Provider

Implement:MessageBodyReader<T>

MessageBodyWriter<T>

Page 34: RESTful Groovy

Summary

JAX-RS 2.0 specificationJersey reference implementation

Hypermedia done through links

Groovy simplifies codeNo major changes unless custom providerHttpBuilder client


Top Related