How do I use Spring Boot to serve static content located in Dropbox folder?
You can add your own static resource handler (it overwrites the default), e.g.
@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/dropbox/");
}
}
There is some documentation about this in Spring Boot, but it's really just a vanilla Spring MVC feature.
Also since spring boot 1.2 (I think) you can simply set spring.resources.staticLocations
.
Springboot (via Spring) now makes adding to existing resource handlers easy. See Dave Syers answer. To add to the existing static resource handlers, simply be sure to use a resource handler path that doesn't override existing paths.
The two "also" notes below are still valid.
. . .
[Edit: The approach below is no longer valid]
If you want to extend the default static resource handlers, then something like this seems to work:
@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class CustomWebMvcAutoConfig extends
WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String myExternalFilePath = "file:///C:/Temp/whatever/m/";
registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);
super.addResourceHandlers(registry);
}
}
The call to super.addResourceHandlers
sets up the default handlers.
Also:
- Note the trailing slash on the external file path. (Depends on your expectation for URL mappings).
- Consider reviewing the source code of WebMvcAutoConfigurationAdapter.