spring-mvc - Spring Rest Docs 如何在我的代码片段或 .adoc 文件中包含应用程序名称

标签 spring-mvc spring-boot asciidoctor spring-restdocs

我正在尝试将 Spring Rest Docs 包含在 Spring Boot 应用程序中,并设法生成了一个简单的 asciidoc HTML 文件,其中显示了应用程序根目录“/”的请求路径。问题是代码段中的请求 URL 不包含应用程序名称?例如,我的应用程序称为“myservice”,所以我希望将根“/”请求路径记录为

$ curl 'http://localhost:8080/myservice/'

相反,我只能生成

$ curl 'http://localhost:8080/'

我是 Spring Rest Docs 的新手,不知道如何在记录的 URL 中包含应用程序名称。它是在 asccidoctor 的 maven 插件中,在测试类的 @before 或 @Test 方法中还是在 .adoc 文件中设置为 'include' 标记的一部分?

最佳答案

这是 mentioned in the documentation :

To configure a request’s context path, use the contextPath method on MockHttpServletRequestBuilder.

在您的示例中,您的应用程序名为 myservice,您正在请求 /。您的 MockMvc 调用应如下所示:

this.mockMvc.perform(get("/myservice/").contextPath("/myservice"))
        .andExpect(status().isOk())
        .andDo(document("index"));

关于spring-mvc - Spring Rest Docs 如何在我的代码片段或 .adoc 文件中包含应用程序名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360281/

相关文章:

java - 如何在 Spring Boot Controller 中检索查询参数?

java - SpringFox,带有 JpaRepository 的 Spring Data Rest - Swagger 无法识别带有 spring boot 的 Rest 存储库

java - 使用 gson 为 WebClient 设置自定义编码器/解码器或 typeAdapter

gradle - 当定义了多个后端时,asciidoctorj-pdf会产生ArrayIndexOutOfBoundsException

java - Spring 休息文档。生成片段时无效的 UTF-8 中间字节

java - 使用 POST 请求时 Spring MVC 无法在 Controller 中找到方法

java - Spring 安全: issues 403 after authorization with single granted

java - 无法在 spring 框架中将 freemarker 模板的编码更改为 UTF-8

java - Spring Boot 自定义登录 : "login", 模板可能不存在,或者可能无法被任何配置的模板解析器访问

asciidoc:在 Code Fence 中包含粗体