3

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?

2 Answers2

4

This is mentioned in the documentation:

To configure a request’s context path, use the contextPath method on MockHttpServletRequestBuilder.

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"));
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Withe the solution described above I get the following error: `equestURI [/] does not start with contextPath` http://stackoverflow.com/questions/35435631/spring-rest-mock-context-path Can you help me out? – Georg Heiler Feb 16 '16 at 17:07
  • As Andy explained above (and also in the example below), you should add the context path in your URI AND in the contextPath method. – Dieter Hubau Oct 19 '16 at 09:18
1

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());

    }
}
sunshine
  • 158
  • 1
  • 10