Spring RestDocs为前端生成页面

时间:2018-03-24 16:42:56

标签: java spring java-8 spring-restdocs spring-auto-restdocs

我的Spring RestApi应用程序使用RestDoc成功生成片段,但我无法使用片段自动生成要在前端运行的页面。 http://localhost/docs返回404,并且在static / docs /

中没有生成html

到目前为止,我的pom.xml看起来像这样

<dependency>
   <groupId>org.springframework.restdocs</groupId>
   <artifactId>spring-restdocs-mockmvc</artifactId>
   <scope>test</scope>
</dependency>
        <plugins>

            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.3</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                            <outputDirectory>
                                ${project.build.outputDirectory}/static/docs
                            </outputDirectory>
                            <attributes>
                                <snippets>${project.build.directory}/generated-snippets</snippets>
                            </attributes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>

我的Junit Api测试看起来像这样

@AutoConfigureMockMvc
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AppLoader.class)
public class ApiDocumentationJUnit5IntegrationTest {
    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    private ObjectMapper objectMapper = new ObjectMapper();

    private RestDocumentationResultHandler documentationHandler;

    @Before
    public void setUp() {
        this.documentationHandler = document("{method-name}",
                preprocessRequest(removeHeaders("Authorization")),
                preprocessResponse(prettyPrint()));
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
            .apply(documentationConfiguration(this.restDocumentation))
        .build();
    }
    @Test
    public void responseCodeTest() throws Exception {
        this.mockMvc.perform(
            get("/cms/status")
                .accept(MediaType.APPLICATION_JSON)
        )
        .andExpect(status().isOk())
        .andDo(
            document("status")
        );
    }

    @Test
    public void retrieveDocumentTest() throws Exception {
        this.mockMvc.perform(get("/api/active", 1L))
            .andExpect(status().isOk()).andDo(
                document("active")
            );
    }

}

0 个答案:

没有答案