如何在 Spring REST 文档中更改 curl 片段中的主机

How to change host in curl snippet in Spring REST Docs

Spring REST Docs 生成一个 curl 片段,这在测试时非常方便。它等同于其文档中所述的 MockMvc 调用,但如果可以将其主机部分替换为测试服务器的主机名(包括端口)而不是 localhost,那就更好了。用现在的版本可以实现吗?

您可以在创建 MockMvc 实例时配置主机(以及方案和端口):

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
        .apply(documentationConfiguration(this.restDocumentation).uris()
                .withScheme("https")
                .withHost("example.com")
                .withPort(443))
        .build();

使用 SpringBoot 和自动配置可以是:

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc    
@AutoConfigureRestDocs(uriScheme = "https", uriHost = "myhost", uriPort = "80")
public class Test { 

    @Autowired
    MockMvc mockMvc;

    ...