Issue
I'm trying to build an app (with Spring boot) that will generate files (.js files), which have to be publicly available. These files (after being created) should be accessible by the link on my server and insertable into someone else's website (with a link). Something similar to how 'jQuery' or 'Google Adwords' work (how they are connected with a html script tag). So the problem i'm facing (or just a lack of knowledge) is how to create these files dynamically and make them accessible publicly.
The idea in general is: 1 user has 1 folder (inside of resources/files folder for example) (let's name it folder1232). Inside this folder his specific js files are generated (for example 111.js). So i want him to be able to access his file by requesting app.com/files/folder1232/111.js URL. What will be the way of doing it? Please let me know if something is unclear.
I'm currently playing with resources folder to understand how it works generally. What I see is that files are somehow mapped on startup (so for example if I create a new folder with a file in runtime, it's not accessible until I restart the app). I also tried to use WebMvcConfigurer, but I think I'm missing something basic.
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/files/**")
.addResourceLocations("file:/files/")
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
I expect to access newly created files (in runtime) from browser. Also would be perfect if i could be able to access newly created folders (in runtime) as well. But atm I have to restart an app to access newly created files and folders.
UPDATE: What i tried to do next, is to create a folder outside my java project. Like this: /Users/%username%/Documents/fileFold And changed my addResourceHandler function to:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/files/**")
.addResourceLocations("file:/Users/%username%/Documents/fileFold/")
.setCachePeriod(0)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
Now it works perfectly. Thanks everyone! NOTE: Don't forget the last slash in your addResourceLocations function, mapping will not work without it (didnt work for me at least).
Solution
So what worked for me specifically: I created a folder to store files OUTSIDE the project. e.g. /Users/%username%/Documents/fileFold
And changed my addResourceHandlers function to
registry.addResourceHandler("/files/**")
.addResourceLocations("file:/Users/%username%/Documents/fileFold/")
.setCachePeriod(0)
.resourceChain(true)
.addResolver(new PathResourceResolver());
NOTE: Don't forget the last slash in your addResourceLocations function, mapping will not work without it (didnt work for me at least).
Answered By - Leopold Stotch