Accessing a Resource in a Spring Boot Application
I have a Spring Boot Web Application (Embedded Jetty Server). All java code and resources are packaged inside a JAR and deployed to the server. However, there is a JSON file outside of the JAR, but in the classpath. those.
my classpath: / usr / local / myserver /
JSON file is located at: /usr/local/myserver/hello.json The
JAR is located at: /usr/local/myserver/app.jar
When I find the url: https: // IP: port / hello.json it gives me "404 file not found". The IP address shown is the server where all files and JARs are present. I believe the default resource path is / static / .. which is inside the JAR. Any file in this static directory is available.
I configured:
@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
public class StaticResourceConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/hello.json").addResourceLocations("file://localhost/usr/local/myserver/");
}
How can I access the JSON file?
If the file is JSON
already in classpath
, then why don't you use a prefix classpath:
like
classpath:/usr/local/myserver/
if you use a prefix file:/
then it loads JSON file
using the url class, so basically you need to give the absolute location of the path like
if these are windows
file:/usr/local/myserver
You need to use the "file: //" prefix + the absolute destination on your filesystem "/ usr / local / myserver /".
registry.addResourceHandler("/hello.json")
.addResourceLocations("file:///usr/local/myserver/");