我有一个Spring boot web应用程序(嵌入式Jetty服务器)。所有的Java代码和资源都打包在JAR中并部署在服务器上。但是,有一个JSON文件在JAR外,但在类路径中。
即:
我该如何访问JSON文件?
我的类路径是:/usr/local/myserver/
JSON文件位于:/usr/local/myserver/hello.json
JAR文件位于:/usr/local/myserver/app.jar
当我访问URL:https://IP:port/hello.json时,它会给出“404 文件未找到”错误。提到的IP是所有文件和JAR所在的服务器。我相信默认的资源路径是/static/..,其中包含在那个'static'目录中的任何文件都是可访问的。
我已经配置:
@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/");
}
我该如何访问JSON文件?