Serving Static resources from file system | Spring Boot Web
Asked Answered
T

4

27

Using a Spring Boot web application I trying to serve my static resource from a file system folder outside my project.

Folder structure looks like:-

          src
             main
                 java
                 resources
             test
                 java
                 resources
          pom.xml
          ext-resources   (I want to keep my static resources here)
                 test.js

Spring Configuration:-

@SpringBootApplication
public class DemoStaticresourceApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(DemoStaticresourceApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/test/**").addResourceLocations("file:///./ext-resources/")
                .setCachePeriod(0);
    }
}

Hitting 'http://localhost:9999/test/test.js' in my browser gives back a 404.

How should I configure ResourceHandlerRegistry to serve static resources from the above mentioned 'ext-resources' folder?

I should be able to switch cache on/off for dev/prod environment.

Thanks

UPDATE 1

Giving absolute file path works:-

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/test/**")
                .addResourceLocations(
                        "file:///C:/Sambhav/Installations/workspace/demo-staticresource/ext-resources/")
                .setCachePeriod(0);
}

How can I provide relative location? Absolute path will make my life tough during build & deploy process.

Tlaxcala answered 17/2, 2015 at 6:54 Comment(3)
have you tried absolute path? (and maybe you just have one / more in your URL :))Welton
Absolute path works. See updated question. Is there any way of providing relative path?Tlaxcala
build it :) e.g. File("ext-resource").getAbsoluteFile().toURI... check javadoc for proper syntaxWelton
P
38

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.

Providenciaprovident answered 17/2, 2015 at 10:35 Comment(2)
FYI... WebMvcConfigurerAdapter has been deprecated in 5.0 of spring. They recommend using WebMvcConfigurer for your mvc configuration.Instalment
I'll note that the paths are relative to where where the app was started, not to where the jar file resides. Example: for java -jar ./target/app.jar, relative paths start above the target directory.Erminna
S
2

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/";
}
Search answered 25/8, 2015 at 8:16 Comment(0)
A
2

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
Abert answered 9/10, 2015 at 9:54 Comment(0)
R
2

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.

Ribal answered 4/3, 2022 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.