REST by Examples

About Me

  • Software Developer/Architect

  • Work at Rackspace

  • JUG Co-Leader

  • @jokerr

Agenda

Agenda

  • Why REST?
  • Sample Service - Design
  • Sample Service - Code

Why REST?

SOAP?

XML RPC?

Adopting HTTP

HTTP Method In English
GET Retrieve
POST Create
PUT Update
DELETE Delete
Status Code In English
100x Informational
200x Received and understood
300x Redirect
400x PEBKAC or client error
500x BOOM!

PUT vs PATCH

The REST Maturity Model

HATEOAS

Example Service

SunnyJ's Cook Book

  • Create and manage recipes

  • Recipes have

    • Steps (turn oven on)

    • Ingredients (1 cup milk)

    • Tagging (cookies, peanuts)

  • Users can search for recipes by tag

The Hardest Part

The Fun Part

JAX-RS

Java API for RESTful Services

  • API Spec for REST

  • Currently on version 2.0

  • Many impliementers including Jersey, RestEasy, and Apache CFX

Annotations

  • @Path

  • @GET, @PUT, @POST, @DELETE

  • @Produces

  • @Consumes

  • @PathParam

  • @QueryParam

JAX-RS

  • ExceptionMappers

  • Marshaling/Unmarshalling

  • Providers

  • Context

Spring

Spring

  • Very popular framework (ecosystem) for Java

  • There's a project for that

  • Create stand-alone apps quickly with Spring Boot

Annotations

  • @RequestMapping

    • path=

    • method=

    • produces=

    • consumes=

  • @RequestParam

  • @RequestBody/ResponseBody

Comparing Annotations

@Path("/recipes")
@GET
@PUT
@POST
@DELETE
@PathParam("id")
@QueryParam("tag")
@FormParam("xyz")
@Produces("application/json")
@Consumes("application/json")
N/A
N/A
@RequestMapping(path="/recipes")
@RequestMapping(method=RequestMethod.GET)
@RequestMapping(method=RequestMethod.PUT)
@RequestMapping(method=RequestMethod.POST)
@RequestMapping(method=RequestMethod.DELETE)
@PathVariable("id")
@RequestParam("tag")
@RequestParam(value="xyz")
@RequestMapping(produces="application/json")
@RequestMapping(consumes="application/json")
@RequestBody
@ResponseBody

Q&A

References

REST By Example

By Josh Kerr

REST By Example

  • 80
Loading comments...

More from Josh Kerr