Spring Rest Api Documenting
Spring Rest Api Documenting
2021, 21:56
Hypermedia
Spring REST Docs provides support for documenting the links in a hypermedia-based
(https://en.wikipedia.org/wiki/HATEOAS) API. The following examples show how to use it:
1 Configure Spring REST docs to produce a snippet describing the response’s links. Uses the static
links method on org.springframework.restdocs.hypermedia.HypermediaDocumentation .
2 Expect a link whose rel is alpha . Uses the static linkWithRel method on
org.springframework.restdocs.hypermedia.HypermediaDocumentation .
The result is a snippet named links.adoc that contains a table describing the resource’s links.
If a link in the response has a title , you can omit the description from its descriptor and
! the title is used. If you omit the description and the link does not have a title , a failure
occurs.
When documenting links, the test fails if an undocumented link is found in the response. Similarly, the test also
fails if a documented link is not found in the response and the link has not been marked as optional.
If you do not want to document a link, you can mark it as ignored. Doing so prevents it from appearing in the
generated snippet while avoiding the failure described above.
You can also document links in a relaxed mode, where any undocumented links do not cause a test failure. To
do so, use the relaxedLinks method on
org.springframework.restdocs.hypermedia.HypermediaDocumentation . This can be useful when
documenting a particular scenario where you only want to focus on a subset of the links.
Atom: Links are expected to be in an array named links . This is used by default when the content type of
the response is compatible with application/json .
https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/ Page 15 of 47
Spring REST Docs 05.07.2021, 21:56
HAL: Links are expected to be in a map named _links . This is used by default when the content type of the
response is compatible with application/hal+json .
If you use Atom- or HAL-format links but with a different content type, you can provide one of the built-in
LinkExtractor implementations to links . The following examples show how to do so:
1 Indicate that the links are in HAL format. Uses the static halLinks method on
org.springframework.restdocs.hypermedia.HypermediaDocumentation .
If your API represents its links in a format other than Atom or HAL, you can provide your own implementation
of the LinkExtractor interface to extract the links from the response.
JAVA
ppuubblli cc s ttaat iicc LinksSnippet l iin k ss(LinkDescriptor... descriptors) {
rreet uurrn HypermediaDocumentation.links(linkWithRel("self").ignored().optional(),
linkWithRel("curies").ignored()).and(descriptors);
}
By default, Spring REST Docs automatically generates snippets for the body of the request and the body of the
response. These snippets are named request-body.adoc and response-body.adoc respectively.
https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/ Page 16 of 47
Spring REST Docs 05.07.2021, 21:56
JSON
{
"contact": {
"name": "Jane Doe",
"email": "[email protected]"
}
}
1 Configure Spring REST docs to produce a snippet describing the fields in the response payload. To
document a request, you can use requestFields . Both are static methods on
org.springframework.restdocs.payload.PayloadDocumentation .
2 Expect a field with the path contact.email . Uses the static fieldWithPath method on
org.springframework.restdocs.payload.PayloadDocumentation .
The result is a snippet that contains a table describing the fields. For requests, this snippet is named request-
fields.adoc . For responses, this snippet is named response-fields.adoc .
When documenting fields, the test fails if an undocumented field is found in the payload. Similarly, the test also
fails if a documented field is not found in the payload and the field has not been marked as optional.
If you do not want to provide detailed documentation for all of the fields, an entire subsection of a payload can
be documented. The following examples show how to do so:
1 Document the subsection with the path contact . contact.email and contact.name are now
https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/ Page 17 of 47
Spring REST Docs 05.07.2021, 21:56
seen as having also been documented. Uses the static subsectionWithPath method on
org.springframework.restdocs.payload.PayloadDocumentation .
subsectionWithPath can be useful for providing a high-level overview of a particular section of a payload.
You can then produce separate, more detailed documentation for a subsection. See Documenting a Subsection
of a Request or Response Payload.
If you do not want to document a field or subsection at all, you can mark it as ignored. This prevents it from
appearing in the generated snippet while avoiding the failure described earlier.
You can also document fields in a relaxed mode, where any undocumented fields do not cause a test failure. To
do so, use the relaxedRequestFields and relaxedResponseFields methods on
org.springframework.restdocs.payload.PayloadDocumentation . This can be useful when documenting a
particular scenario where you want to focus only on a subset of the payload.
By default, Spring REST Docs assumes that the payload you are documenting is JSON. If you
! want to document an XML payload, the content type of the request or response must be
compatible with application/xml .
JSON
{
"a":{
"b":[
{
"c":"one"
},
{
"c":"two"
},
{
"d":"three"
}
],
"e.dot" : "four"
}
}
https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/ Page 18 of 47
Spring REST Docs 05.07.2021, 21:56
In the preceding JSON payload, the following paths are all present:
Path Value
a An object containing b
You can also document a payload that uses an array at its root. The path [] refers to the entire array. You can
then use bracket or dot notation to identify fields within the array’s entries. For example, [].id corresponds
to the id field of every object found in the following array:
JSON
[
{
"id":1
},
{
"id":2
}
]
You can use * as a wildcard to match fields with different names. For example, users.*.role could be used
to document the role of every user in the following JSON:
https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/ Page 19 of 47
Spring REST Docs 05.07.2021, 21:56
JSON
{
"users":{
"ab12cd34":{
"role": "Administrator"
},
"12ab34cd":{
"role": "Guest"
}
}
}
Type Description
boolean The value of each occurrence of the field is a boolean ( true or false ).
varies The field occurs multiple times in the payload with a variety of different types.
You can also explicitly set the type by using the type(Object) method on FieldDescriptor . The result of the
toString method of the supplied Object is used in the documentation. Typically, one of the values
enumerated by JsonFieldType is used. The following examples show how to do so:
XML payloads
This section describes how to work with XML payloads.
https://docs.spring.io/spring-restdocs/docs/2.0.5.RELEASE/reference/html5/ Page 20 of 47