Spring REST文档-避免记录链接

问题描述 投票:0回答:1

我在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
spring-boot spring-mvc spring-hateoas hateoas spring-restdocs
1个回答
0
投票

links(…)创建一个片段,专门用于记录超媒体链接。由于您根本不想记录任何链接,因此应避免使用links代码段。

相反,请修改responseFields的使用以忽略_links字段及其下方嵌套的所有内容。 REST Docs将此称为小节,可以使用subsectionWithPath(String)进行记录。您想忽略subsectionWithPath(String)小节,因此可以执行以下操作:

_links
© www.soinside.com 2019 - 2024. All rights reserved.