Home > Net >  Serving Spring Rest docs
Serving Spring Rest docs

Time:10-04

I have the following Spring Rest Docs maven configuration, which makes my generated docs end up in WEB-INF/classes/static/docs when I package my Spring Boot app.

 <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>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.restdocs</groupId>
                        <artifactId>spring-restdocs-asciidoctor</artifactId>
                        <version>${spring-restdocs.version}</version>
                    </dependency>
                </dependencies>
            </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}/static/docs
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${project.build.directory}/generated-docs
                                    </directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

then I'm trying to serve them up in this controller (my-api-doc.html is the name of the generated docs)

@Controller
public class DocsController {

    @RequestMapping("/docs")
    private String docs() {
        return "my-api-doc";
    }

}

But I am getting a 404. How can I make my Spring Boot app serve up the html from the WAR file in WEB-INF/classes/static/docs?

CodePudding user response:

My mistake was trying to serve the html from a controller. Files that end up in WEB-INF/classes/static are meant to be accessed directly and are not resolved when controllers look for a view. I was able to access the file directly with my-api/docs/my-api-doc.html

CodePudding user response:

According to the Spring Boot docs, static content should be in one of four predefined locations. /WEB-INF/classes/static is not one of them. Try moving your generated docs to /public or /META-INF/resources.

CodePudding user response:

Try using Swagger (at https://swagger.io/) for documentation. You can actually host the documentation of your APIs and get Updated automatically whenever you update the APIs(this reduces the burden of updating documentation whenever you made an update to APIs).
Also, the static folder is meant to attach View to Spring APIs, I suggest not to save any files in that.

  • Related