簡體   English   中英

Spring REST 文檔 - 避免記錄鏈接

[英]Spring REST Docs - Avoid document the links

我在 SpringBoot 2 應用程序中有這個方法:

@Test
public void shouldEchoTheParameter() throws Exception {
mockMvc.perform(get("/echo").param("echoMessage", "Test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("Test")))
.andDo(document("echo-example",
preprocessResponse(prettyPrint()),
links(linkWithRel("self").ignored().optional()),
requestParameters(
parameterWithName("echoMessage").description("The message to be echoed")),
responseFields(
fieldWithPath("message").
description("The message echoed"))
));
}

我想避免記錄這部分有效負載:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/echo"
    }
  }
}

我包括了這個:

links(linkWithRel("self").ignored().optional()),

但我有這個錯誤:

java.lang.IllegalStateException: No LinkExtractor has been provided and one is not available for the content type application/vnd.pxs.echo.v1+json;charset=UTF-8

links(…)創建一個專門用於記錄超媒體鏈接的片段。 由於您根本不想記錄任何鏈接,因此應避免使用links片段。

相反,修改您對responseFields的使用以忽略_links字段及其下嵌套的所有內容。 REST Docs 將此稱為一個小節,可以使用subsectionWithPath(String)進行記錄。 你想忽略_links小節,所以你會做這樣的事情:

responseFields(subsectionWithPath("_links").ignored(),     
    fieldWithPath("message").description("The message echoed"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM