Slide 1

Slide 1 text

Spring REST Docs DOCUMENTING RESTFUL APIS / @OLIVERGIERKE / @ANKINSON

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Hypermedia Documenting
 APIs?

Slide 4

Slide 4 text

Let’s use
 code
 generation!

Slide 5

Slide 5 text

Meet:
 Swagger…

Slide 6

Slide 6 text

It’s URI centric

Slide 7

Slide 7 text

Baking URIs into clients breaks independent deployability

Slide 8

Slide 8 text

It doesn’t support hypermedia https://github.com/swagger-api/swagger-core/issues/97

Slide 9

Slide 9 text

Always playing catch up – ResponseEntity https://github.com/springfox/springfox/issues/532

Slide 10

Slide 10 text

The writing experience is unpleasant @ApiOperation(value = "Create a new user", notes = "The name " + "of the user must be at least four characters in length.") @ApiResponses({ @ApiResponse(code = 400, message = "Bad request"), @ApiResponse(code = 201, message = "User created")}) @ResponseStatus(HttpStatus.CREATED) @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public HttpHeaders create(@RequestBody UserInput userInput) { … }

Slide 11

Slide 11 text

It isn’t dry @ApiResponses({ @ApiResponse(code=400, message="Bad request"), @ApiResponse(code=201, message="User created")}) @ResponseStatus(HttpStatus.CREATED) public HttpHeaders create(@RequestBody UserInput userInput) { … } @ExceptionHandler(IllegalArgumentException.class) private ResponseEntity handleIllegalArgumentException() { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); }

Slide 12

Slide 12 text

@ApiResponses({ @ApiResponse(code=400, message="Bad request"), @ApiResponse(code=201, message="User created")}) @ResponseStatus(HttpStatus.CREATED) public HttpHeaders create(@RequestBody UserInput userInput) { … } @ExceptionHandler(IllegalArgumentException.class) private ResponseEntity handleIllegalArgumentException() { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } It isn’t dry

Slide 13

Slide 13 text

@ApiResponses({ @ApiResponse(code=400, message="Bad request"), @ApiResponse(code=201, message="User created")}) @ResponseStatus(HttpStatus.CREATED) public HttpHeaders create(@RequestBody UserInput userInput) { … } @ExceptionHandler(IllegalArgumentException.class) private ResponseEntity handleIllegalArgumentException() { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } It isn’t dry

Slide 14

Slide 14 text

@ApiResponses({ @ApiResponse(code=400, message="Bad request"), @ApiResponse(code=201, message="User created")}) @ResponseStatus(HttpStatus.CREATED) public HttpHeaders create(@RequestBody UserInput userInput) { … } @ExceptionHandler(IllegalArgumentException.class) private ResponseEntity handleIllegalArgumentException() { return new ResponseEntity<>(HttpStatus.PAYMENT_REQUIRED); } It’s outright wrong

Slide 15

Slide 15 text

All the problems you have with JavaDoc

Slide 16

Slide 16 text

All the problems you have with JavaDoc — but worse.

Slide 17

Slide 17 text

"When did we start to equate „easy to create“ with „good“? Me

Slide 18

Slide 18 text

"Code generation – so that you can do the wrong thing faster… Kevlin Henney

Slide 19

Slide 19 text

What to
 do instead?

Slide 20

Slide 20 text

Write as much as possible in a format that’s designed for writing

Slide 21

Slide 21 text

Don’t use the implementation
 to provide the documentation blindly

Slide 22

Slide 22 text

Provide some guarantees that the documentation is accurate

Slide 23

Slide 23 text

TDD — Test-Driven Documentation

Slide 24

Slide 24 text

Asciidoctor Spring MVC Test Maven / Gradle spring-projects/spring-restdocs

Slide 25

Slide 25 text

Writing a test case… @Test public void index() throws Exception { this.mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(jsonPath("_links.notes", is(notNullValue()))) .andExpect(jsonPath("_links.tags", is(notNullValue()))) .andDo(document("index")); } 1 2 3 4

Slide 26

Slide 26 text

…creates a Asciidoctor cURL snippet… [source, bash] ---- $ curl 'http://localhost:8080/' -i ----

Slide 27

Slide 27 text

…creates a Asciidoctor cURL snippet…

Slide 28

Slide 28 text

… as well as a response one… [source, http] ---- HTTP/1.1 200 OK Content-Type: application/hal+json { "_links" : { "tags" : { "href" : "http://localhost:8080/tags" }, "notes" : { "href" : "http://localhost:8080/notes" } } ----

Slide 29

Slide 29 text

… as well as a response one…

Slide 30

Slide 30 text

Document links and payloads / responses this.mockMvc.perform(get("/")) .andExpect(status().isOk()) .andDo(document("index", links( linkWithRel("notes").description( "The <>"), linkWithRel("tags").description( "The <>") ), responseFields( fieldWithPath("_links").description( "<> to other resources") ) )); 1 2 3

Slide 31

Slide 31 text

Document links and payloads / responses |=== | Relation | Description | notes | The <> | tags | The <> |===

Slide 32

Slide 32 text

Document links and payloads / responses

Slide 33

Slide 33 text

Document links and payloads / responses |=== |Path|Type|Description |_links |Object |<> to other resources |===

Slide 34

Slide 34 text

Document links and payloads / responses

Slide 35

Slide 35 text

Demo

Slide 36

Slide 36 text

For more bliss, combine with
 HAL browser…

Slide 37

Slide 37 text

Demo

Slide 38

Slide 38 text

Resources Chatty42 — GitHub repository, App on Heroku Spring RESTDocs — GitHub repository Andy Wilkinson - Documenting RESTful APIs — Webinar recording