Serving Static resources from file system | Spring Boot Web
Spring Boot Maven Plugin can add extra directories to the classpath. In your case you could include that in your pom.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<folders>
<folder>${project.build.directory}/../ext-resources</folder>
</folders>
...
</configuration>
</plugin>
So that way you don't need inlcude any hard-code in your classes. Simply start your webapp with
mvn spring-boot:run
This is what I did in the WebConfig class, inside the addResourceHandlers method:
boolean devMode = this.env.acceptsProfiles("development");
String location;
if (devMode) {
String currentPath = new File(".").getAbsolutePath();
location = "file:///" + currentPath + "/client/src/";
} else {
location = "classpath:static/";
}
file:///
is an absolute URL pointing to the root of the filesystem and, therefore, file:///./ext-resources/
means that Spring Boot is looking for resources in a directory named ext-resources
in the root.
Update your configuration to use something like file:ext-resources/
as the URL.
static resources (eg:html js css etc)
can be placed in the same level directory of project or jar, named public
. contents will be servered without additional config.