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

Joh*_*Lim 5 spring-restdocs

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

And*_*son 12

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

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


Max*_*kov 5

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

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

    @Autowired
    MockMvc mockMvc;

    ...
Run Code Online (Sandbox Code Playgroud)