making your api easy to document with spring rest docs

43
Making your API easy to document with Making your API easy to document with Spring REST Docs Spring REST Docs http://gmind7.github.io Java Softeware Developer

Upload: -

Post on 16-Apr-2017

1.908 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Making your API easy to document with Spring REST Docs

Making your API easy to document withMaking your API easy to document withSpring REST Docs Spring REST Docs

http://gmind7.github.ioJava Softeware Developer

Page 2: Making your API easy to document with Spring REST Docs

99

Page 3: Making your API easy to document with Spring REST Docs

TestTest

DocumentDocument

"테스트 작성은 어렵지 않다. 단지 좋아하지 않을 뿐이다."

"잘 작성되고 설명이 풍부하면서 동시에 간결한 문서 작성하기"

http://www.itworld.co.kr/slideshow/85296

Page 4: Making your API easy to document with Spring REST Docs
Page 5: Making your API easy to document with Spring REST Docs
Page 6: Making your API easy to document with Spring REST Docs

*.adoc*.adoc

Page 7: Making your API easy to document with Spring REST Docs

*.adoc*.adoc *.html*.htmlmore

Page 8: Making your API easy to document with Spring REST Docs
Page 9: Making your API easy to document with Spring REST Docs
Page 10: Making your API easy to document with Spring REST Docs

RequestRequestResponseResponse

Page 11: Making your API easy to document with Spring REST Docs
Page 12: Making your API easy to document with Spring REST Docs

index.adocindex.adoc

Page 13: Making your API easy to document with Spring REST Docs

*.adoc

index.adoc

Page 14: Making your API easy to document with Spring REST Docs

*.adoc

index.adoc

Page 15: Making your API easy to document with Spring REST Docs

index.adoc

...........................................................................

spring rest docs.......

curl http://127......................

RESTful API Docuemtns

*.adoc

Page 16: Making your API easy to document with Spring REST Docs

mvc test auto

Page 17: Making your API easy to document with Spring REST Docs
Page 18: Making your API easy to document with Spring REST Docs

Appointment RESTful APIAppointment RESTful APIDocumentsDocuments

Page 19: Making your API easy to document with Spring REST Docs

/doctors/doctors

/patients/patients

/schedules/schedules

Page 21: Making your API easy to document with Spring REST Docs

/ /{ "timestamp": "2015-10-296 10:49:34", "status": 404, "error": "Not Found", "message": "No message available", "path": "/"}

Page 22: Making your API easy to document with Spring REST Docs

/doctors /doctors{ "doctors": [ { "id": 1, "name": "doctor_name_1" }, { "id": 2, "name": "doctor_name_2" }, .................. ]}

Page 23: Making your API easy to document with Spring REST Docs

http://stateless.co/hal_specification.html

HATEOASHATEOASHypermedia as the Engine of Application State

Page 24: Making your API easy to document with Spring REST Docs

/ /{ "_links": { "doctors": { "href": "http://localhost:8080/doctors" }, "patient": { "href": "http://localhost:8080/patients" }, "schedule": { "href": "http://localhost:8080/schedules" } }}

Page 25: Making your API easy to document with Spring REST Docs

/doctors /doctors{ "_links": { "self": { "href": "http://localhost:8080/doctors{?page,size,sort}" "templated": true }, "next": { "href": "http://localhost:8080/doctors?page=1&size=10{&sort}" "templated": true } }, "_embedded": { "doctors": [ { "id": 1, "name": "doctor_name_1", "_links": { "self": { "href": "http://127.0.0.1:8080/doctors/1" },

Page 26: Making your API easy to document with Spring REST Docs

Richardson Maturity Model Richardson Maturity Model

http://martinfowler.com/articles/richardsonMaturityModel.html

Page 27: Making your API easy to document with Spring REST Docs

plugins { id "org.asciidoctor.convert" version "1.5.2"}dependencies { testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc:1.0.0.RELEASE'}ext { snippetsDir = file('build/generated-snippets')}test { outputs.dir snippetsDir}asciidoctor { attributes 'snippets': snippetsDir inputs.dir snippetsDir dependsOn test }jar { dependsOn asciidoctor from ("${asciidoctor.outputDir}/html5") { into 'static/docs'

Page 28: Making your API easy to document with Spring REST Docs

@Rulepublic final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets");

@Autowiredprivate WebApplicationContext context;

public RestDocumentationResultHandler document;

public MockMvc mockMvc;

@Beforepublic void setUp() { this.document = document("{method-name}"); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation) .uris().withScheme("http").withHost("root-endpoint" .alwaysDo(this.document) .build();}

Page 29: Making your API easy to document with Spring REST Docs

@Testpublic void doctorsShowAll() throws Exception { this.mockMvc.perform(get("/doctors").param("page","2").param("size" .andExpect(status().isOk()) .andDo(this.document.snippets( links( linkWithRel("next").optional().description("다음페이지" linkWithRel("prev").optional().description("이전페이지" linkWithRel("self").description("현재페이지")), requestParameters( parameterWithName("page").description("페이지 번호"), parameterWithName("size").description("리스트 사이즈")), responseFields( fieldWithPath("_links").type(JsonFieldType.OBJECT).description( fieldWithPath("_embedded.doctors").type(JsonFieldType.OBJECT).description( fieldWithPath("page").type(JsonFieldType.OBJECT).description(}

Page 30: Making your API easy to document with Spring REST Docs

:test:test

:asciidoctor:asciidoctor

:jar:jar

Page 31: Making your API easy to document with Spring REST Docs

index.adoc

...........................................................................

spring rest docs.......

curl http://127......................

RESTful API Docuemtns

*.adoc

Page 32: Making your API easy to document with Spring REST Docs

index.adoc

overview.adoc

resources.adoc

overview-current.adoc..........*.adoc.....*.adoc

resources-doctors.adoc..........*.adoc.....*.adoc

Page 33: Making your API easy to document with Spring REST Docs

C:\spring-rest-docs-seminar\src\main\asciidoc>tree /F

C:.│ index.adoc│├─overview│ overview-current-version.adoc│ overview-schema.adoc│ overview-parameters.adoc│ overview-root-endpoint.adoc│ ...........│ .......│ ..│├─resources│ resources-doctors.adoc│ resources-index.adoc│ resources-patients.adoc│ resources-schedules.adoc

Page 34: Making your API easy to document with Spring REST Docs

C:\spring-rest-docs-seminar\build\generated-snippets>tree /F

C:.├─doctors-show-all│ curl-request.adoc│ http-request.adoc│ http-response.adoc│ links.adoc│ request-parameters.adoc│ response-fields.adoc│└─doctors-show-one curl-request.adoc http-request.adoc http-response.adoc links.adoc path-parameters.adoc response-fields.adoc..............................

Page 35: Making your API easy to document with Spring REST Docs

C:.├─doctors-show-all│ curl-request.adoc│ http-request.adoc│ http-response.adoc│ links.adoc│ request-parameters.adoc│ response-fields.adoc

C:\spring-rest-docs-seminar\src\main\asciidoc>tree /F

C:.│ index.adoc│├─overview│ overview-current-version.adoc│ overview-schema.adoc│ overview-parameters.adoc│ overview-root-endpoint.adoc│ ...........│ .......│ ..│├─resources│ resources-doctors.adoc│ resources-index.adoc│ resources-patients.adoc│ resources-schedules.adoc

Page 36: Making your API easy to document with Spring REST Docs

mvc test auto

Page 37: Making your API easy to document with Spring REST Docs

overview.html

resources.html

overview-current.html..........*.html.....*.html

resources-doctors.html..........*.html.....*.html

index.html

Page 38: Making your API easy to document with Spring REST Docs

build/asciidocbuild/asciidoc

/generated-sn~ /generated-sn~

src/main/asciidocsrc/main/asciidoc

src/main/resourcessrc/main/resources

/static/docs/static/docs

Page 39: Making your API easy to document with Spring REST Docs

http://gmind7.github.io/docs.htmlhttp://gmind7.github.io/docs.html

Page 40: Making your API easy to document with Spring REST Docs
Page 41: Making your API easy to document with Spring REST Docs

build.gradle || pom.xmlbuild.gradle || pom.xml

Spring MVC TESTSpring MVC TEST

.andDo(Spring REST Docs) .andDo(Spring REST Docs)

index.adoc index.adoc

include::resources-*.adoc include::resources-*.adoc

include::**-restdocs.adoc include::**-restdocs.adoc

index.htmlindex.html

Page 42: Making your API easy to document with Spring REST Docs

https://github.com/gmind7https://github.com/gmind7

/spring-rest-docs-seminar/spring-rest-docs-seminar

Page 43: Making your API easy to document with Spring REST Docs

Making your API easy to document withMaking your API easy to document withSpring REST Docs Spring REST Docs

The End.The End.