9. Sample Application
• Spring Data REST :
• https://github.com/spring-projects/spring-restdocs/tree/master/samples/rest-notes-spring-data-rest
• Spring HATEOAS :
• https://github.com/spring-projects/spring-restdocs/tree/master/samples/rest-notes-spring-hateoas
• HATEOAS : Hypermedia As the Engine Of Application State
10. • api-guide.adoc : API guide
• ApiDocumentation.java
• getting-started-guide.adoc : getting started guide
• GettingStartedDocumentation.java
• Spring Data REST
16. Generating documentation
snippets
• Spring REST Docs은 MVC Test framework을 사용
• MVC test framework doc
• http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#spring-mvc-test-framework
17. Setting up Spring MVC Test
@Rule
public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets");
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
1) snippets을 생성하기위해 @Rule annotation
을 이용, RestDocumentation 을 선언, “build/
generated-snippets” pom.xml 에서 선언되
어있음
2) MockMvc instance를 생성키위해 @Before에서 setUp()
method 선언, MockMvc의 instance는
RestDocumentationMockMvcConfigurer 를 사용해서 설정할 수
있는데, static documentationConfiguration() method로 얻을
수 있다. documentationConfiguration의 method들?
18. @Rule
public final RestDocumentation restDocumentation = new RestDocumentation("build/generated-snippets");
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
documentationConfiguration은 URIs 설정할 수 있는데, 아래와 같이
사용이 가능하다.
documentationConfiguration(this.restDocumentation).uris()
.withScheme(“https”)
.withHost("example.com")
.withPort(443)
32. public void example() {
ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserInput.class);
List<String> descriptions = userConstraints.descriptionsForProperty("name");
}
static class UserInput {
@NotNull
@Size(min = 1)
String name;
@NotNull
@Size(min = 8)
String password;
1) ConstraintDescriptions의 instance를 생성
2) name property의 constraint를 가져온다. List 에는 NotNull
조건과 Size 조건이 있다.
Bean Validation 1.1의 constraints 모두 지원한다.
AssertFalse, AssertTrue, DecimalMax, DecimalMin, Digits,
Future, Max, Min, NotNull, Null, Past, Pattern, Size
33. .andDo(document("create-user", requestFields(
attributes(
key("title").value("Fields for user creation")),
fieldWithPath("name")
.description("The user's name")
.attributes(
key("constraints").value("Must not be null. Must not be empty")),
fieldWithPath("email")
.description("The user's email address")
.attributes(
key("constraints").value("Must be a valid email address")))));
request field의 extra information으로
constraints를 문서화도 가능함
name field에 “constraints” 속성을 설정할 수 있다.
35. 8) Using parameterised output directories
• output 디렉토리를 위해 아래 parameter를 지원.
_ ( userscore) 를 사용
-(dash) 를 사용
36. @Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(document("{method-name}/{step}/"))
.build();
} alwaysDo 를 사용하고, method-name이
creatingANote 라면, 디렉토리는 creating-a-note/1/
이라는 이름에 snippet이 저장된다.
37. 9) Customising the output
• 9-1) Spring REST Docs : Mustache template를 사
용
• ./spring-restdocs-core/build/resources/main/org/springframework/restdocs/templates/
*.snippet
• curl-request.adoc 을 위해 template 을 사용하려면,
• src/test/resources/org/springframework/restdocs/templates/curl-request.snippet 을 만들어
서 사용하면 됨.
38. .andDo(document("create-user", requestFields(
attributes(
key("title").value("Fields for user creation")),
fieldWithPath("name")
.description("The user's name")
.attributes(
key("constraints").value("Must not be null. Must not be empty")),
fieldWithPath("email")
.description("The user's email address")
.attributes(
key("constraints").value("Must be a valid email address")))));
9-2) including extra information
1) attributes() method를 사용하고, “title” attribute를 설정
2) “name” 의 constraints를 설정
3) “email” 의 constraints를 설정
custom template : request-fields.snippet 설정
39. custom template : request-fields.snippet
.{{title}}
|===
|Path|Type|Description|Constraints
{{#fields}}
|{{path}}
|{{type}}
|{{description}}
|{{constraints}}
{{/fields}}
|===
1) 테이블에 title 추가
2) 새로운 column “Constraints” 추가
3) 테이블의 각 row 에 “constraints” 속성의 설명을 포함
46. Working with Asciidoctor
• asciidoctor syntax
• http://asciidoctor.org/docs/asciidoc-syntax-quick-reference/
• asciidoctor user manual
• http://asciidoctor.org/docs/user-manual
47. 1) including snippets
• 특정 snippet을 포함시킬 수 있음.
• include::{snippets}/index/curl-request.adoc[]
48. 2) Customising tables
• Formatting columns
• Configuring the title
• User manual :
• http://asciidoctor.org/docs/user-manual/#tables
51. spring-boot 설치
• 추천 버전
• Spring Boot v1.1.4 release
• Mac OS X 환경에 설치
$ brew tap pivotal/tap
$ brew install springboot
http://docs.spring.io/autorepo/docs/spring-boot/1.1.4.RELEASE/reference/html/getting-started-installing-spring-boot.html
52. java & mvn & intelliJ
• 최소 스펙
• Java : 1.6 +
• maven : 3.0 +
• Mac OS X
• java : 1.7.0_79
• maven : 3.2.5
• intelliJ IDEA 14.1.5+
54. …
[[overview-errors]]
== Errors
Whenever an error response (status code >= 400) is returned, the body will contain a JSON object
that describes the problem. The error object has the following structure:
include::{snippets}/error-example/response-fields.adoc[]
For example, a request that attempts to apply a non-existent tag to a note will produce a
`400 Bad Request` response:
include::{snippets}/error-example/http-response.adoc[]
[[overview-hypermedia]]
…
api-guide.doc
55.
56. MockHttpServletRequest:
HTTP Method = GET
Request URI = /error
Parameters = {}
Headers = {}
Handler:
Type = org.springframework.boot.autoconfigure.web.BasicErrorController
Method = public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>>
org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
MockHttpServletResponse:
Status = 400
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {
"timestamp" : 1447748756746,
"status" : 400,
"error" : "Bad Request",
"message" : "The tag 'http://localhost:8080/tags/123' does not exist",
"path" : "/notes"
}
Forwarded URL = null
Redirected URL = null
Cookies = []
Process finished with exit code 0
IntelliJ Output :
57. ~/spring-restdocs/samples/rest-notes-spring-data-rest/target/generated-snippets/error-example$ ls -la
total 32
drwxr-xr-x 6 EricAhn staff 204 Nov 17 17:25 .
drwxr-xr-x 3 EricAhn staff 102 Nov 17 17:25 ..
-rw-r--r-- 1 EricAhn staff 63 Nov 17 17:25 curl-request.adoc
-rw-r--r-- 1 EricAhn staff 60 Nov 17 17:25 http-request.adoc
-rw-r--r-- 1 EricAhn staff 287 Nov 17 17:25 http-response.adoc
-rw-r--r-- 1 EricAhn staff 340 Nov 17 17:25 response-fields.adoc
58. $ cd spring-restdocs/samples/rest-notes-spring-data-rest/src/main/asciidoc
$ cp -R target/generated-snippets/error-example .
$ ls
drwxr-xr-x 7 EricAhn staff 238 Nov 17 22:14 .
drwxr-xr-x 5 EricAhn staff 170 Nov 12 15:21 ..
-rw-r--r-- 1 EricAhn staff 5985 Nov 17 22:23 api-guide.adoc
drwxr-xr-x 6 EricAhn staff 204 Nov 17 22:12 error-example
-rw-r--r-- 1 EricAhn staff 5888 Nov 12 15:21 getting-started-guide.adoc