你能相信用谷歌搜索这个结果会有两个惊人的结果吗?

我正在try 使用Spring rest Docs(spring-restdocs-mockmvc)为我的服务生成API文档.

Running on a macbook pro with M2 processor. I've read that this issue happens because / is the root directory on a mac, and that is read only. I don't know why that path is being used though.
I have the system property org.springframework.restdocs.outputDir set, and when I check it the path is correct: /Users/me/workspace/my-service/target/generated-snippets.

我的代码如下所示:

@SpringBootTest()
@ExtendWith(RestDocumentationExtension.class)
class ExampleControllerTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;

    @BeforeEach
    public void setUp(RestDocumentationContextProvider restDocumentation) {
        System.out.println("Output Directory: " + System.getProperty("org.springframework.restdocs.outputDir"));
        this.mvc = MockMvcBuilders.webAppContextSetup(this.context)
                                      .apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation))
                                      .build();
    }

    @Test
    public void index() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
           .andExpect(status().isOk()).andExpect(content().string(containsString("\"name\":\"Example Service\"")))
           .andDo(MockMvcRestDocumentation.document("/",
                                                    responseFields(
                                                            fieldWithPath("name").description("The name of the service"),
                                                            fieldWithPath("list").description("A list of Integers"),
                                                            fieldWithPath("calledTimes").description(
                                                                    "A count of the number of times this resource has been called"),
                                                            fieldWithPath("deployEnvironment").description(
                                                                    "The environment the service is deployed to")
                                                    )
                  )
           );
    }
}

我完全不知道在这里该做什么.

When I pause the code with the debugger I can see that the RestDocumentationContextProvider passed into the setup method has a value called outputDirectory that is null. So it seems to me that it's not picking up the system property for some reason.
If I look in valuesStore.storedValues there is one key/value pair with the key class org.springframework.restdocs.ManualRestDocumentation and a value that contains (among other things) outputDirectory set to target/generated-snippets.

推荐答案

这个问题是由于您在拨打MockMvcRestDocumentation.document("/")时使用/造成的.Javadoc将其描述为"正在记录的API调用的标识符".参考文档describes在创建所生成的摘录文件的名称时如何使用该标识符.

当识别符以/开头时,它是already absolute so the configured output directory is ignored.我建议使用类似indexroot的内容作为标识.这将导致在配置的输出目录中创建名为indexroot的目录.该目录将包含您正在记录的API操作的所有代码片段.

Java相关问答推荐

是否有一种格式模式,可以在除0之外的数字前面有正负符号?

具有默认分支的JUnit代码覆盖率切换声明

Java取消任务运行Oracle查询通过JDBC—连接中断,因为SQLSTATE(08006),错误代码(17002)IO错误:套接字读取中断

ittext pdf延迟签名,签名无效

路径映射未发生

为什么我的ArrayList索引的索引总是返回-1?

Java中的死锁及其重入锁和锁

如何从Keyloak映射Hibernate实体中的用户

如何在Application.yaml中连接字符串?

基于接口的投影、原生查询和枚举

使用多个RemoteDatabase对象的一个线程

如何在一行中使用Dijkstra中的Java Stream

如何从日志(log)行中删除包名称?

在Java中将int[]矩阵添加到ArrayList中,但出现错误

使用htmlunit和java单击按钮

为什么Collectors.toList()不能保证易变性

如何在列表(链表)中插入一个新 node (作为prelast)

为什么没有加载java.se模块?

如何使用jOOQ在PostgreSQL中从枚举类型生成Java枚举

转换为JSON字符串时,日期按天递减-Java