RESTFul

RESTFul

Representational State Transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services.
— wikipedia

Richardson Maturity Model

Richardson Maturity Model

Doamin Model

Diagram

Level 0: The Swamp of POX

The starting point for the model is using HTTP as a transport system for remote interactions, but without using any of the mechanisms of the web.
Richardson Maturity Model
— Martin Fowler

Level 0

  1. Use one or a few URL endpoint to receive all request

  2. Send all request by one HTTP method, mostly POST

  3. Always response HTTP status 200 regardless of sucess or fail

  4. Contain verbs in request body

  5. Contain status in response body

Level 0

POST /api/level0/operation HTTP/1.1
Content-Type: application/json;charset=UTF-8
Accept: application/json
Host: localhost:8080
Content-Length: 86

{
  "command" : "createProduct",
  "parameters" : {
    "title" : "Test product 123"
  }
}

Level 0

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 164

{
  "status" : "SUCCESS",
  "message" : "",
  "parameters" : {
    "id" : 3,
    "title" : "Test product 123",
    "createdAt" : "Sun Mar 11 15:43:11 CST 2018"
  }
}

Level 1: Resources

The first step towards the Glory of Rest in the RMM is to introduce resources. So now rather than making all our requests to a singular service endpoint, we now start talking to individual resources.
Richardson Maturity Model
— Martin Fowler

Level 1

  1. Expose multiple URL endpoints

  2. Send all request by one HTTP method, mostly POST

  3. Always response HTTP status 200 regardless of sucess or fail

  4. Contain verbs in request body

  5. Contain status in response body

  6. Define resources which used in many reqeuest/response payload

Level 1

POST /api/level1/product HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:8080
Content-Length: 117

{
  "command" : "create",
  "data" : {
    "id" : null,
    "title" : "Test product 123",
    "createdAt" : null
  }
}

Level 1

HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 160

{
  "status" : "SUCCESS",
  "message" : null,
  "data" : {
    "id" : 5,
    "title" : "Test product 123",
    "createdAt" : "2018-03-11T07:43:11.840+0000"
  }
}

Level 2: HTTP Verbs

Level 2 moves away from this, using the HTTP verbs as closely as possible to how they are used in HTTP itself.
Richardson Maturity Model
— Martin Fowler

Level 2: HTTP Verbs

  1. Expose multiple URL endpoints

  2. Each URL endpoint only accept fixed structure request/reposne payload

  3. Define resources which used in many reqeuest/response paylaod

  4. Use HTTP Verbs to represent action

  5. Use HTTP Status to represent result

Level 2: HTTP Verbs

POST /api/level2/product HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:8080
Content-Length: 73

{
  "id" : null,
  "title" : "New product title",
  "createdAt" : null
}

Level 2

HTTP/1.1 201 Created
Content-Type: application/json;charset=UTF-8
Content-Length: 97

{
  "id" : 21,
  "title" : "New product title",
  "createdAt" : "2018-04-30T12:02:08.667+0000"
}

Level 3: Hypermedia Controls

The point of hypermedia controls is that they tell us what we can do next, and the URI of the resource we need to manipulate to do it. Rather than us having to know where to post our appointment request, the hypermedia controls in the response tell us how to do it.
Richadson Maturity Model
— Martin Fowler

Level 3: Hypermedia Controls

Diagram

Level 3:

  1. Expose multiple URL endpoints

  2. Each URL endpoint only accept fixed structure request/reposne payload

  3. Define resources which used in many reqeuest/response paylaod

  4. Use HTTP Verbs to represent action

  5. Use HTTP Status to represent result

  6. Tell us links to other resources in response

Level 3

POST /products HTTP/1.1
Accept: application/json
Content-Type: application/json;charset=UTF-8
Content-Length: 365
Host: localhost:8080

{
  "title" : "New Product",
  "tags" : [ "Electronics", "Mobile" ],
  "images" : [ "http://localhost:8080/productImages/5d958ccadf86bd1cd947dbc6", "http://localhost:8080/productImages/5d958ccadf86bd1cd947dbc7" ],
  "variants" : [ "http://localhost:8080/productVariants/5d958ccadf86bd1cd947dbc9", "http://localhost:8080/productVariants/5d958ccadf86bd1cd947dbca" ]
}

Level 3

HTTP/1.1 201 Created
Content-Length: 589
Content-Type: application/json;charset=UTF-8
Location: http://localhost:8080/products/5d958ccadf86bd1cd947dbcc

{
  "title" : "New Product",
  "tags" : [ "Electronics", "Mobile" ],
  "createdAt" : "2019-10-03T05:53:14.592+0000",
  "updatedAt" : "2019-10-03T05:53:14.592+0000",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc"
    },
    "product" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc"
    },
    "images" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc/images"
    },
    "variants" : {
      "href" : "http://localhost:8080/products/5d958ccadf86bd1cd947dbcc/variants"
    }
  }
}

Q&A