I have a question regarding Spring boot. I'd like to serve static content, and I have made a ResourceHandler
in my configuration:
@Configuration
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
private static final Logger LOG = LoggerFactory.getLogger(WebApplicationConfig.class);
@Bean
public FileSystem fileSystem() {
FileSystem fileSystem = new LocalFileSystem();
return fileSystem;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
LOG.info("Serving static content from " + fileSystem().getBaseFolder());
registry.addResourceHandler("/static/storage/**").addResourceLocations("file:///" + fileSystem().getBaseFolder());
super.addResourceHandlers(registry);
}
}
My application runs in a Docker container and I mounted the host' volume /myapplication/storage. When I ssh into my Docker container, I see that this volume is present and that reflects the fileSystem().getBaseFolder()
-call. This directory is also writable, but it is outside my project.
The LOG statement is executed: "Serving static content from /myapplication/storage"
, so I know this code is being executed as well.
Further information that can be relevant is:
@RestController
's annotated. All the methods in there start with @RequestMapping("/api/")
, so there is no mapping that starts with /static. Does anyone know how do I make those files in that folder readable. It is always returning a 404. The folder must be outside of the project, so don't suggest to put it into /resources/static
.
Any help is appreciated.
Ok, I made a beginners mistake. As @chrylis mentioned in the comments, I removed the ///
in front of file:
. I did some test runs, and as it appeared, I had the following entry in my application.properties
: myapplication.picture.basefolder=/myapplication/storage
. It missed a trailing slash. So I updated it to myapplication.picture.basefolder=/myapplication/storage/
, and my problem was fixed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.