Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Best of REST (JAX 2018)

Tom Hombergs
April 24, 2018
280

Best of REST (JAX 2018)

A tour through some of the major decisions we have to make when implementing a HTTP API (or even a REST API).

Tom Hombergs

April 24, 2018
Tweet

Transcript

  1. The Swamp of Plain Old XML POST / Client Server

    <?xml version="1.0"?> <request> <action>createBook</action> <payload> <title>…</title> </payload> </request>
  2. Resources Client Server POST /books POST /books/42?action=update {"title": "Hitchhiker's Guide

    to The Galaxy"} { "action": "create", "payload": { "title": "Hitchhiker's Guide to The Galaxy" } }
  3. HTTP Verbs Client Server POST /books PUT /books/42 {"title": "Hitchhiker's

    Guide to The Galaxy"} {"title": "Hitchhiker's Guide to The Galaxy"}
  4. Links and Relations { "bookList": [{ "title": "...", "_links": {

    "self": { "href": "/books/42" } } } } GET /books
  5. @SpringBootApplication @EnableHypermediaSupport(type = HypermediaType.HAL) public class MyApplication{ public static void

    main(String[] args) { SpringApplication.run(MyApplication.class, args); } } Hypermedia with Spring Boot
  6. @GetMapping(path = "books/{id}") public ResponseEntity getBook(@PathVariable Long id) { Book

    book = bookRepository.findOne(id); BookResource result = bookAssembler.toResource(book); return new ResponseEntity(result, HttpStatus.OK); } Hypermedia with Spring Boot
  7. Generating Docs with Spring Rest Docs document("books/create", links(halLinks(), linkWithRel("self") .description("Link

    to the created book."), requestFields( fields.withPath("title") .description("The title of the book.")));
  8. Generating Docs with Spring Rest Docs document("books/create", links(halLinks(), linkWithRel("self") .description("Link

    to the created book."), requestFields( fields.withPath("title") .description("The title of the book.")));
  9. @Test public void createProjectSuccessfully() throws Exception { ProjectResource project =

    projectResource().validProjectResource(); mvc().perform(post("/books") .content(toJsonWithoutLinks(project)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()) .andExpect(containsResource(ProjectResource.class)) .andDo(document(...)); } Generating Docs from Tests
  10. POST /orders PUT /orders/4711 PUT /orders/4711 Fat Resource { "orderId":

    4711, "orderItems": [ { "articleId": 0815, ... } ] }
  11. { "orderId": 4711, "orderItems": [ { "articleId": 0815, ... }

    ] } POST /orders POST /orders/4711/items PUT /orders/4711/items Nested Resource DELETE /orders/4711/items
  12. { "orderId": 4711, "orderItemCount": 2 } GET /orders/4711?view=summary Resource Views

    GET /orders/4711/view/summary GET /orders/4711 Accepts: application/vnd+adesso.orderSummary+json
  13. { "orderId": 4711, "status": "COMPLETE" , "orderItems": [ { "articleId":

    0815, ... } ] } PUT /orders/4711 State Change
  14. JWT create signedtoken Client Server login withusername/ password returntoken send

    token witheach request check token signature read user informationfromtoken returnresponse
  15. JWT