Slides of the talk on documenting RESTful APIs that I gave at SpringOne 2GX 2015
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/SPRINGONE2GXWASHINGTON, DCDocumenting RESTful APIsAndy Wilkinson@ankinson
View Slide
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/How RESTful is your API?2
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/REST maturity model3Hyper mediaHTTP verbsSeparateResourcesXML-RPCSOAPRPCLevel 0 Level 1 Level 2 Level 3http://www.crummy.com/writing/speaking/2008-QCon/act3.htmlhttp://martinfowler.com/articles/richardsonMaturityModel.html
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/How RESTful do you want it to be?4
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Making your API easy to document5
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Consistency6HTTP status codesHTTP verbs
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Uniqueness7Use distinct relations on your links
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/8"_links" : {"self" : {"href" : "http://localhost:8080/"},"notes" : {"href" : "http://localhost:8080/notes"},"tags" : {"href" : "http://localhost:8080/tags"}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/9"_links" : {"self" : {"href" : "http://localhost:8080/tags/3"},"tagged-notes" : {"href" : "http://localhost:8080/tags/3/notes"}}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/What should you document?10
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Cross-cutting concerns11HTTP status codesHTTP verbs (PUT vs PATCH)Authentication and authorisationRate limiting…
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Resources12What do the represent?What input do they accept?What output do the produce?
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Links13Where can you go?What will you find when you get there?
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/What shouldn’t you document?14
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/URIs15
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/What does it look like whenyou get it wrong?16Learning from my mistakes
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/17
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/And when you get it right?18
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/19
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/RESTful API documentation tools20
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Swagger21http://swagger.iospringfox/springfox
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/You get a lot for little effort22
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/But…23
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/It doesn’t support hypermedia24https://github.com/swagger-api/swagger-core/issues/97
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/It’s URI centric25
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Always playing catch up – ResponseEntity26https://github.com/springfox/springfox/issues/532
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Always playing catch up – HttpHeaders27
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/The writing experience is unpleasant28@RequestMapping(method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)@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)public HttpHeaders create(@RequestBody UserInput userInput) {…}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/It isn’t dry29@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);}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/It isn’t dry30@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);}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/It isn’t dry31@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);}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Swagger2Markup32Swagger2Markup/swagger2markup
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/HAL and HAL Browser33http://stateless.co/hal_specification.htmlmikekelly/hal-browser
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/HAL Browser demo34
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Spring REST Docs35spring-projects/spring-restdocshttp://projects.spring.io/spring-restdocs
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Write as much as possible in a format that’sdesigned for writing36
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Don’t use the implementation to provide thedocumentation37
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Provide some guarantees that thedocumentation is accurateTDD – Test-Driven Documentation38
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/AsciidoctorSpring MVC TestMaven or Gradle39spring-projects/spring-restdocs
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/40@Testpublic 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"));}54321
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/41[source,bash]----$ curl 'http://localhost:8080/' -i----
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/42
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/43[source,http]----HTTP/1.1 200 OKContent-Type: application/hal+json{"_links" : {"tags" : {"href" : "http://localhost:8080/tags"},"notes" : {"href" : "http://localhost:8080/notes"}}
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/44
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/45this.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"))));432156
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/46|===| Relation | Description| notes| The <>| tags| The <>|===
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/47
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/48|===|Path|Type|Description|_links|Object|<> to other resources|===
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/49
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Spring REST Docs demo50
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Thank you51@ankinsonwilkinsona[email protected]