Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Writing Comprehensive and Guaranteed Up-to-date REST API Documentation By Andreas Evers @andreasevers

Slide 2

Slide 2 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Keep the documentation accurate 2

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Document in the right place 3 Controller Jackson Response Entity HTTP response POJO

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Keep it DRY 4 @ResponseStatus(CREATED) HttpHeaders create( @RequestBody UserInput userInput) { … } @ExceptionHandler( IllegalArgumentException.class) ResponseEntity illegalArgument() { return new ResponseEntity<>(BAD_REQUEST); } @ApiResponses({ @ApiResponse(code=400, message="…"), @ApiResponse(code=201, message="…")}) return new ResponseEntity<>(BAD_REQUEST);

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Write docs in markdown or asciidoc 5

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 6 generated snippets manually written template generated HTML integration
 tests Icons created by Richard Slater, Ryan Choi, Guillaume Bahri, iconsmind.com from Noun Project

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Integration test 7 this.mockMvc.perform(get("/cars/{carId}","1")) .andExpect(status().isOk()) .andDo(document("cars", pathParameters( parameterWithName("carId") .description("Id of the car")), responseFields( fieldWithPath("brand") .description("Brand of the car")) .and(this.otherFields)));

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Request snippets 8 [source,bash] ---- $ curl 'http://localhost:8080/cars/1' -i ---- [source,bash] ---- $ http GET 'http://localhost:8080/cars/1' ---- Curl Request HTTPie Request (new in 1.1)

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Request snippets 9 [source,http,options="nowrap"] ---- GET /cars/1 HTTP/1.1 Host: localhost ---- HTTP Request

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Request path parameters snippet 10 ./cars/{carId} |=== |Parameter|Description |carId |Id of the car |===

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Response snippet 11 [source,http,options=“nowrap"] ---- HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Content-Length: 150 { "brand" : "BMW", "constructionYear" : 2010 } ----

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ All snippets 12 Request - Curl request - HTTP request - HTTPie request Request fields Request path parameters Request parameters Request headers Request parts (new in 1.1) Response Response fields Hypermedia links Response headers Response fields

Slide 13

Slide 13 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Documentation template 13 [[resources-car-get]] === Retrieving a car For retrieving a car, the following path parameter has to be used to indicate which car to retrieve: include::{snippets}/car-get/path-parameters.adoc[] For example, a curl request for the first car in the repository looks like this: include::{snippets}/car-get/curl-request.adoc[]

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Documentation HTML 14

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Demo 15

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Easy to package the documentation
 in your project’s jar file 16

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Easy to customize the request or response
 before it is documented 17

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Support for reusing common documentation 18

Slide 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Easy to add extra information to the snippets 19

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Easy to document constraints of fields 20

Slide 21

Slide 21 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Easy to ignore parts of the request or response
 during documentation 21

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Possibility to switch off failing
 on inaccurate documentation 22

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Easy to mark parts of the request or response
 as optional 23

Slide 24

Slide 24 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Support for both JSON and XML 24

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Support for REST Assured 25

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Ideal for brownfield stubbing 26

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Thank you for your attention @andreasevers http://projects.spring.io/spring-restdocs/ https://github.com/spring-projects/spring-restdocs @springcentral spring.io/blog @pivotal pivotal.io/blog @pivotalcf http://engineering.pivotal.io