【问题标题】:Spring rest docs generated content not found未找到 Spring rest docs 生成的内容
【发布时间】:2016-11-09 16:33:43
【问题描述】:

我正在使用 Spring REST Docs 为我们的 API 生成文档。 我已将教程中的所有内容添加到 build.gradle http://docs.spring.io/spring-restdocs/docs/current/reference/html5/

ext {
    snippetsDir = file('build/generated-snippets')
}

test {
    outputs.dir snippetsDir
}

asciidoctor {
    attributes 'snippets': snippetsDir
    inputs.dir snippetsDir
    outputDir "build/asciidoc"
    dependsOn test
    sourceDir 'src/main/asciidoc'
}

jar {
    dependsOn asciidoctor
    from ("${asciidoctor.outputDir}/html5") {
        into 'static/docs'
    }
}

在我执行gradle build 之后,我可以看到在build/asciidoc 目录中生成了文件,也在build/generated-snippets 中生成了文件。

但是当我从 IDEA gradle 任务 bootRun 运行并尝试访问 localhost:8080/docs/index.html 时,我没有找到 404。只是为了测试,我尝试将一些 index.html 文件放在下面resources/static 目录,然后执行bootRun,之后我可以访问localhost:8080/index.html 文件。

如果我打开我的 .jar 文件,我可以看到目录 BOOT-INF/classes/static/docs 下的静态文件,因此它们被打包到 jar 中。

也许有人有同样的问题?

【问题讨论】:

  • 使用您显示的 Gradle 配置,bootRun 不会运行您的测试或从您的 Asciidoctor 生成 HTML。
  • 可以用bootRun生成吗?

标签: java spring spring-mvc spring-restdocs


【解决方案1】:

您需要做两件事,以便在使用 bootRun 时提供文档。第一种是将生成的文档复制到bootRun 使用的类路径上的位置:

task copyRestDocs(type: Copy) {
    dependsOn asciidoctor
    from "${asciidoctor.outputDir}/html5"
    into "${sourceSets.main.output.resourcesDir}/static/docs"
}

请注意,这个新任务依赖于asciidoctor 任务。这可确保文档在被复制之前已经生成。

其次,bootRun 任务必须依赖于新的copyRestDocs 任务:

bootRun {
    dependsOn copyRestDocs
}

【讨论】:

  • +1 不利的一面是,如果测试用例需要很长时间才能完成,bootRun 也需要一些时间来初始化。
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
相关资源
最近更新 更多