私のアプリは と を使用spring-data-restしてspring-restdocsいます。私のセットアップは本当に標準的です。ドキュメントからほぼ完全にコピーされていますが、何か不足している場合に備えて、以下のサンプルを含めました. 私の mvc テストを実行すると、次のように失敗します。
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"_links" : {
"self" : {
"href" : "https://my-api/item/10"
},
"item" : {
"href" : "https://my-api/item/10"
}
}
}
これは私のテストコードです:
@Rule
public JUnitRestDocumentation restDocs = new JUnitRestDocumentation("target/generated-snippets");
// ...
mockMvc = webAppContextSetup(wac) //WebApplicationContext
.apply(documentationConfiguration(restDocs)
.uris()
.withHost("my-api")
.withPort(443)
.withScheme("https"))
.build();
// ....
mockMvc.perform(get("/items/{id}", "10"))
.andDo(documentation)
スタックは次のとおりです。
at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:176)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:100)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.example.my.api.domain.MyRepositoryRestTest.findOne(MyRepositoryRestTest.java:36)
上手にプレイするにはどうすればいいですspring-restdocsか?spring-data-rest
編集:
私のdocumentationインスタンスは次のように定義されています。
ResultHandler documentation = document("items/findOne",
preprocessRequest(prettyPrint(), maskLinks()),
preprocessResponse(prettyPrint()),
responseFields(
fieldWithPath("name").description("Item name.")
// Bunch more
));
@meistermeier が示したように (そして、リンクを無視するための restdocs ドキュメントに従って、追加できます
links(linkWithRel("self").ignored(),
linkWithRel("_self").ignored().optional()) // docs suggest this. /shrug
しかし、それでも私には次のことが残っています:
SnippetException: 次の関係を持つリンクは文書化されていませんでした: [item]
_links常に同じエンティティへの自己参照を持っているようですよね? 次のように、すべてのテストでエンティティ固有のリンクを無視せずに、これをきれいに処理するにはどうすればよいですか。
links(linkWithRel("item").ignored())
上記の行を追加しても (すべてのフィールドがすべておよびself _self curies/またはになるように)、テストの結果はこの質問の上部にある元のエラーに戻ります。itemignored()optional()