Documenting RESTful APIs

51
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/ SPRINGONE2GX WASHINGTON, DC Documenting RESTful APIs Andy Wilkinson @ankinson

Transcript of Documenting RESTful APIs

Page 1: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

SPRINGONE2GXWASHINGTON, DC

Documenting RESTful APIsAndy Wilkinson

@ankinson

Page 2: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

How RESTful is your API?

2

Page 3: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

REST maturity model

3

Hypermedia

HTTPverbs

Separate Resources

XML-RPC SOAPXML-RPC

Level 0 Level 1 Level 2 Level 3

http://www.crummy.com/writing/speaking/2008-QCon/act3.htmlhttp://martinfowler.com/articles/richardsonMaturityModel.html

Page 4: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

How RESTful do you want it to be?

4

Page 5: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Making your API easy to document

5

Page 6: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Consistency

6

HTTP status codesHTTP verbs

Page 7: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Uniqueness

7

Use distinct relations on your links

Page 8: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 8

"_links" : { "self" : { "href" : "http://localhost:8080/" }, "notes" : { "href" : "http://localhost:8080/notes" }, "tags" : { "href" : "http://localhost:8080/tags" }

Page 9: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 9

"_links" : { "self" : { "href" : "http://localhost:8080/tags/3" }, "tagged-notes" : { "href" : "http://localhost:8080/tags/3/notes" }}

Page 10: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

What should you document?

10

Page 11: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Cross-cutting concerns

11

HTTP status codesHTTP verbs (PUT vs PATCH)

Authentication and authorisationRate limiting

Page 12: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Resources

12

What do the represent?What input do they accept?

What output do the produce?

Page 13: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Links

13

Where can you go?What will you find when you get there?

Page 14: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

What shouldn’t you document?

14

Page 15: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

URIs

15

Page 16: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

What does it look like whenyou get it wrong?

16

Learning from my mistakes

Page 17: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 17

Page 18: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

And when you get it right?

18

Page 19: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 19

Page 20: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

RESTful API documentation tools

20

Page 21: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Swagger

21

http://swagger.io

springfox/springfox

Page 22: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

You get a lot for little effort

22

Page 23: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

But…23

Page 24: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

It doesn’t support hypermedia

24

https://github.com/swagger-api/swagger-core/issues/97

Page 25: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

It’s URI centric

25

Page 26: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Always playing catch up – ResponseEntity

26

https://github.com/springfox/springfox/issues/532

Page 27: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Always playing catch up – HttpHeaders

27

Page 28: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

The writing experience is unpleasant

28

@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) {

…}

Page 29: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

It isn’t dry

29

@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<Void> handleIllegalArgumentException() {

return new ResponseEntity<>(HttpStatus.BAD_REQUEST);}

Page 30: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

It isn’t dry

30

@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<Void> handleIllegalArgumentException() {

return new ResponseEntity<>(HttpStatus.BAD_REQUEST);}

Page 31: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

It isn’t dry

31

@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<Void> handleIllegalArgumentException() {

return new ResponseEntity<>(HttpStatus.BAD_REQUEST);}

Page 32: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Swagger2Markup

32

Swagger2Markup/swagger2markup

Page 33: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

HAL and HAL Browser

33

http://stateless.co/hal_specification.html

mikekelly/hal-browser

Page 34: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

HAL Browser demo

34

Page 35: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Spring REST Docs

35

spring-projects/spring-restdocs

http://projects.spring.io/spring-restdocs

Page 36: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

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

36

Page 37: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Don’t use the implementation to provide the documentation

37

Page 38: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Provide some guarantees that the documentation is accurate

TDD – Test-Driven Documentation

38

Page 39: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

AsciidoctorSpring MVC TestMaven or Gradle

39

spring-projects/spring-restdocs

Page 40: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/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"));

}5

4

3

2

1

Page 41: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 41

[source,bash]----$ curl 'http://localhost:8080/' -i----

Page 42: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 42

Page 43: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/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" }}

Page 44: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 44

Page 45: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 45

this.mockMvc.perform(get("/")) .andExpect(status().isOk()) .andDo(document("index", links( linkWithRel("notes").description( "The <<resources-notes, Notes resource>>"), linkWithRel("tags").description( "The <<resources-tags, Tags resource>>") ), responseFields( fieldWithPath("_links").description( "<<resources-index-links,Links>> to other resources") )));

4

3

2

1

5

6

Page 46: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 46

|===| Relation | Description

| notes| The <<resources-notes,Notes resource>>

| tags| The <<resources-tags,Tags resource>>|===

Page 47: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 47

Page 48: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 48

|===|Path|Type|Description

|_links|Object|<<resources-index-links,Links>> to other resources

|===

Page 49: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/ 49

Page 50: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Spring REST Docs demo

50

Page 51: Documenting RESTful APIs

Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/

Thank you

51

@ankinson

wilkinsona

[email protected]