Spring RestDocs
를 실습하고 있는데 글 아래에 첨부한 예외가 발생했다.
예외 메세지를 읽어보고 공식 문서도 확인해봤다. 원인은 메시지에서 말하는 그대로였다. responseFields
메소드를 사용하는데 응답의 필드 중에서 문서화하지 않은 부분이 있었기 때문이었다.
responseFields()
정의public static ResponseFieldsSnippet responseFields(FieldDescriptor... descriptors)
Returns a Snippet that will document the fields of the API operation's response payload. The fields will be documented using the given descriptors .
If a field is present in the response payload, but is not documented by one of the descriptors, a failure will occur when the snippet is invoked. Similarly, if a field is documented, is not marked as optional, and is not present in the response, a failure will also occur. For payloads with a hierarchical structure, documenting a field with a subsection descriptor will mean that all of its descendants are also treated as having been documented.
If you do not want to document a field or subsection, a descriptor can be configured to ignore it. The ignored field or subsection will not appear in the generated snippet and the failure described above will not occur.
Parameters:
descriptors - the descriptions of the response payload's fields
Returns:
the snippet that will document the fields
See Also:
fieldWithPath(String), subsectionWithPath(String)
해결 방법은 두 가지다.
예외 메시지 그대로 해결하면 된다. 문서화되지 않은 필드를 문서화하면 된다. 정확한 문서를 위해 이 방법을 권장한다.
relaxedResponseFields
메소드를 사용해서 해결하는 방법이 있다. 이 메서드는 문서화하지 않는 필드는 무시한다. 응답 필드 일부분만 문서화할 수 있다는 장점이 있지만, 이로 인해 정확한 문서를 생성하지 못 한다. 유의해서 사용해야 한다.
- `relaxedResponseFields` 정의
public static ResponseFieldsSnippet relaxedResponseFields(FieldDescriptor... descriptors)
Returns a Snippet that will document the fields of the API operation's response payload. The fields will be documented using the given descriptors .
If a field is documented, is not marked as optional, and is not present in the request, a failure will occur. Any undocumented fields will be ignored.
Parameters:
descriptors - the descriptions of the response payload's fields
Returns:
the snippet that will document the fields
Since:
1.2.0
See Also:
fieldWithPath(String), subsectionWithPath(String), beneathPath(String)
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"limitOfEnrollment" : 200,
"_links" : {
"self" : {
"href" : "http://localhost:8080/api/events/1"
},
"update-event" : {
"href" : "http://localhost:8080/api/events/1"
},
"query-events" : {
"href" : "http://localhost:8080/api/events"
}
}
}
...