|
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring REST Docs 4.0.0! |
Documented URIs
This section covers configuring documented URIs.
MockMvc URI Customization
When using MockMvc, the default configuration for URIs documented by Spring REST Docs is as follows:
| Setting | Default |
|---|---|
Scheme |
|
Host |
|
Port |
|
This configuration is applied by MockMvcRestDocumentationConfigurer.
You can use its API to change one or more of the defaults to suit your needs.
The following example shows how to do so:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
@ExtendWith(RestDocumentationExtension.class)
class CustomUriConfiguration {
// Fields ...
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@BeforeEach
void setUp(RestDocumentationContextProvider restDocumentation) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(restDocumentation).uris()
.withScheme("https")
.withHost("example.com")
.withPort(443))
.build();
}
}
| If the port is set to the default for the configured scheme (port 80 for HTTP or port 443 for HTTPS), it is omitted from any URIs in the generated snippets. |
To configure a request’s context path, use the contextPath method on MockHttpServletRequestBuilder.
|
WebTestClient URI Customization
When using WebTestClient, the default base for URIs documented by Spring REST Docs is localhost:8080.
You can customize this base by using the baseUrl(String) method on WebTestClient.Builder.
The following example shows how to do so:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.documentationConfiguration;
@ExtendWith(RestDocumentationExtension.class)
class CustomUriConfiguration {
// Fields ...
private WebTestClient webTestClient;
@Autowired
private ApplicationContext context;
@BeforeEach
void setUp(RestDocumentationContextProvider restDocumentation) {
this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
.configureClient()
.baseUrl("https://api.example.com") (1)
.filter(documentationConfiguration(restDocumentation))
.build();
}
}
| 1 | Configure the base of documented URIs to be api.example.com. |