Reputation: 83
I'm trying to include Spring Rest Docs with a Spring Boot application and have managed to generate a simple asciidoc HTML file showing the request path to the application root '/'. The problem is that the request URL in the snippet does not include the application name? For Example my application is called 'myservice' so I'd like the root '/' request path to be documented as
$ curl 'http://localhost:8080/myservice/'
instead I can only generate
$ curl 'http://localhost:8080/'
I'm new to Spring Rest Docs and cannot work out how to include the application name in the documented URLs. Is it set in the maven plugin for asccidoctor, in the @before or @Test methods of the test class or in the .adoc file as part of the 'include' tag?
Upvotes: 3
Views: 1897
Reputation: 166
The way i got it to work is to include the context-path both as part of the rest URI and also calling the contextPath on the RequestBuilder. Here is the working example. In my example "api" was the context root. I had to include it the request and also call contextPath method on the RequestBuilder and set it.
@Andy Wilkinson : I know this might be repeating what you said but thought a complete working example would be more clear for people like me :)
@RunWith(MockitoJUnitRunner.class)
public class UserControllerTest {
private UserController controller;
private MockMvc mockMvc;
@Rule
public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
@Before
public void setUp() {
controller = new UserController();
mockMvc = standaloneSetup(controller).apply(documentationConfiguration(this.restDocumentation)).build();
}
@Test
public void testUsers() throws Exception {
this.mockMvc.perform(get("/api/users/{userId}","1234").contextPath("/api"))
.andExpect(status().isOk())
.andDo(document("user-information", pathParameters(
parameterWithName("userId").description("user id.")
), responseFields(
fieldWithPath("firstName").description("first name of the user. "),
fieldWithPath("lastName").description("Last name of the user. ")
)))
.andDo(print());
}
}
Upvotes: 1
Reputation: 116091
This is mentioned in the documentation:
To configure a request’s context path, use the
contextPath
method onMockHttpServletRequestBuilder
.
In your example your application is called myservice
and you're requesting /
. Your MockMvc call should look something like this:
this.mockMvc.perform(get("/myservice/").contextPath("/myservice"))
.andExpect(status().isOk())
.andDo(document("index"));
Upvotes: 4