我有一个使用Spring REST文档的springBoot 2.1.9.RELEASE应用程序。
我在TestController中有此方法
@Test
public void createOK() throws Exception {
String content = ResourceUtils.getResourceFileAsString ("/new_hostel.json");
mockMvc.perform(post("/hostel")
.content(content)
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("create-hostel",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
links(halLinks(),
linkWithRel("ld:GetHostel").
description("Get Hostel"),
linkWithRel("curies").
description("Documentation")),
requestFields(
fieldWithPath("description").description("The description"),
fieldWithPath("name").description("The name"),
fieldWithPath("id").description("The id")
)
));
}
但是运行时出现此错误:
org.springframework.restdocs.snippet.ModelCreationException: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
at [Source: (byte[])""; line: 1, column: 0]
at org.springframework.restdocs.hypermedia.LinksSnippet.createModel(LinksSnippet.java:127)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:81)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:201)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:200)
at com.bendiciones.buenas.noches.HostelControllerIT.createOK(HostelControllerIT.java:88)
链接摘录记录了响应中的超媒体链接。根据错误判断,来自控制器的响应为空,因此没有指向文档的链接。您应该更新测试以删除对POST
请求的响应中的链接文档,或者更新处理该控制器的控制器方法以返回包含某些链接的主体。