Orest
Orest

Reputation: 6748

Spring rest docs generated content not found

I'm using Spring REST Docs to generate documentation for our API. I've added everything to build.gradle from tutorial here 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'
    }
}

After I do gradle build I can see that in build/asciidoc directory files are generated and also in build/generated-snippets.

But when I run from IDEA gradle task bootRun and trying to access localhost:8080/docs/index.html I'm getting not found 404. Just for test I've tried to put some index.html file under resources/static directory and then do bootRun and I can access localhost:8080/index.html file after that.

If I open my .jar file I can see static files under directory BOOT-INF/classes/static/docs so they are packed into jar.

Maybe somebody had the same issue?

Upvotes: 2

Views: 2617

Answers (1)

Andy Wilkinson
Andy Wilkinson

Reputation: 116051

There are two things that you need to do so that the documentation is served when using bootRun. The first is to copy the generated documentation into a location that's on the classpath used by bootRun:

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

Note that this new task depends on the asciidoctor task. This ensures that the documentation has been generated before it's copied.

Secondly, the bootRun task must depend on the new copyRestDocs task:

bootRun {
    dependsOn copyRestDocs
}

Upvotes: 6

Related Questions