pathParameters 文档异常(找不到 urlTemplate)

pathParameters documentation exception (urlTemplate not found)

当使用 pathParameters 记录 URI 路径参数时,如下所示

@Test
public void documentGetRouteById() throws Exception {
    this.mockMvc.perform(get("/route/{id}", "FooBar")).andExpect(status().isOk())
            .andDo(document("api-getRouteById",
                    pathParameters(parameterWithName("id").description("die Routen ID"))));
}

我得到以下异常

java.lang.IllegalArgumentException: urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request?
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.restdocs.request.PathParametersSnippet.extractUrlTemplate(PathParametersSnippet.java:95)
    at org.springframework.restdocs.request.PathParametersSnippet.extractActualParameters(PathParametersSnippet.java:82)
    at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:77)
    at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:65)
    at org.springframework.restdocs.request.PathParametersSnippet.createModel(PathParametersSnippet.java:67)
    at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
    at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:101)
    at org.springframework.test.web.servlet.MockMvc.andDo(MockMvc.java:158)

我很确定我按照说明进行了测试设置 here

我可能做错了什么?

(Spring REST 文档版本为 1.0。0.BUILD-SNAPSHOT)

异常消息试图为您指出正确的方向:

urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request?

您需要使用 RestDocumentationRequestBuilders 以便 Spring REST Docs 可以捕获 URL 并从中提取参数。这是 mentioned in the documentation 上面写着:

To make the path parameters available for documentation, the request must be built using one of the methods on RestDocumentationRequestBuilders rather than MockMvcRequestBuilders.

MockMvcRequestBuilders.get 的静态导入替换为 RestDocumentationRequestBuilders.get 的静态导入应该可以解决问题。