如何将spring-restdocs包含在war文件中?

时间:2016-04-15 04:23:16

标签: spring-boot spring-restdocs

将生成的spring-restdocs包含在war文件中的步骤是什么?

1 个答案:

答案 0 :(得分:0)

按照以下步骤,我能够将休息文档集成到war文件中。

  1. 按照spring website上的说明配置项目以生成doc文件,并编写生成文档的测试用例。初始设置完成后,下一步是将文档集成到war文件中,您可以按照以下说明操作:

  2. 创建SpringMvcConfiguration

        @EnableWebMvc
    
        public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
    
         private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/resources/" };
         @Override
         public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }
          }
        }
    
  3. 更新pom.xml以包含ascii doc,copy-resources

    的插件
                  <build>
                  .....
                      <plugins>
                            <plugin>
                            <groupId>org.asciidoctor</groupId>
                            <artifactId>asciidoctor-maven-plugin</artifactId>
                            <version>1.5.2</version>
                            <executions>
                                <execution>
                                    <id>generate-docs</id>
                                    <phase>prepare-package</phase>
                                    <goals>
                                        <goal>process-asciidoc</goal>
                                    </goals>
                                    <configuration>
                                        <backend>html</backend>
                                        <doctype>book</doctype>
                                        <sourceDirectory>src/main/asciidoc</sourceDirectory>
                                        <attributes>
                                            <snippets>${snippetsDirectory}</snippets>
                                        </attributes>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <artifactId>maven-resources-plugin</artifactId>
                            <version>2.7</version>
                            <executions>
                                <execution>
                                    <id>copy-resources</id>
                                    <phase>prepare-package</phase>
                                    <goals>
                                        <goal>copy-resources</goal>
                                    </goals>
                                    <configuration>
                                        <outputDirectory>
                                            ${project.build.outputDirectory}/public/docs
                                        </outputDirectory>
                                        <resources>
                                            <resource>
                                                <directory>
                                                    ${project.build.directory}/generated-docs
                                                </directory>
                                            </resource>
                                        </resources>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build> 
    

    插件条目负责生成文档并将其添加到target / classes文件夹。在这种情况下,在target / classes / public / docs下。

    1. 最后,在部署war文件时,文档可在http://:// docs /下找到 在我的例子中,我创建了index.adoc文件,它生成了index.html作为最终输出。