Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导访问静态资源缺少scr/main/resources_Java_Spring_Spring Boot_Resources_Spring Properties - Fatal编程技术网

Java Spring引导访问静态资源缺少scr/main/resources

Java Spring引导访问静态资源缺少scr/main/resources,java,spring,spring-boot,resources,spring-properties,Java,Spring,Spring Boot,Resources,Spring Properties,我正在开发一个Spring启动应用程序。我需要在开始时解析一个XML文件(countries.XML)。问题是我不知道把它放在哪里,这样我就可以访问它。 我的文件夹结构是 ProjectDirectory/src/main/java ProjectDirectory/src/main/resources/countries.xml 我的第一个想法是将它放在src/main/resources中,但是当我尝试创建文件(countries.xml)时,我得到了一个NPE,stacktrace显示我

我正在开发一个Spring启动应用程序。我需要在开始时解析一个XML文件(countries.XML)。问题是我不知道把它放在哪里,这样我就可以访问它。 我的文件夹结构是

ProjectDirectory/src/main/java
ProjectDirectory/src/main/resources/countries.xml
我的第一个想法是将它放在src/main/resources中,但是当我尝试创建文件(countries.xml)时,我得到了一个NPE,stacktrace显示我的文件在ProjectDirectory中查找(因此src/main/resources/没有添加)。我试图创建一个文件(resources/countries.xml),路径看起来像ProjectDirectory/resources/countries.xml(因此同样没有添加src/main)

我尝试添加这个,但没有结果

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    super.addResourceHandlers(registry);
}
我知道我可以手动添加src/main/manual,但我想了解为什么它不能正常工作。我还尝试了ResourceLoader的例子——同样没有结果

有人能告诉我问题出在哪里吗

更新: 仅供将来参考-在构建项目后,我遇到了访问文件的问题,所以我将文件更改为InputStream

InputStream is = new ClassPathResource("countries.xml").getInputStream();
只需使用弹簧式

只要这个文件在类路径上的某个地方,Spring就会找到它。在开发和测试期间,这可以是
src/main/resources
。在生产中,它可以是当前正在运行的目录

编辑:如果文件位于fat JAR中,此方法不起作用。在这种情况下,您需要使用:

InputStream is = new ClassPathResource("countries.xml").getInputStream();

要获取类路径中的文件,请执行以下操作:

Resource resource = new ClassPathResource("countries.xml");
File file = resource.getFile();
要在启动时读取文件,请使用
@PostConstruct

@Configuration
public class ReadFileOnStartUp {

    @PostConstruct
    public void afterPropertiesSet() throws Exception {

        //Gets the XML file under src/main/resources folder
        Resource resource = new ClassPathResource("countries.xml");
        File file = resource.getFile();
        //Logic to read File.
    }
}

下面是一个用于在Spring Boot应用程序启动时读取XML文件的示例。

在使用Spring Boot应用程序时,使用
resource.getFile()
将类路径资源部署为JAR是很困难的,因为我遇到了同样的问题。 这个扫描可以通过使用Stream来解决,Stream将找出类路径中任何位置的所有资源

下面是相同代码的代码片段-

ClassPathResource classPathResource = new ClassPathResource("fileName");
InputStream inputStream = classPathResource.getInputStream();
content = IOUtils.toString(inputStream);

您需要使用以下构造

InputStream in = getClass().getResourceAsStream("/yourFile");

请注意,您必须在文件名之前添加此斜杠。

您可以使用以下代码从资源文件夹中读取字符串形式的文件

final Resource resource = new ClassPathResource("public.key");
String publicKey = null;
try {
     publicKey = new String(Files.readAllBytes(resource.getFile().toPath()), StandardCharsets.UTF_8);
} catch (IOException e) {
     e.printStackTrace();
}

我使用spring boot,因此我可以简单地使用:

File file = ResourceUtils.getFile("classpath:myfile.xml");

我使用Spring Boot,我的解决方案是

"src/main/resources/myfile.extension"
希望它能帮助别人

由于java.net.URL不足以处理所有类型的低级资源,Spring引入了org.springframework.core.io.Resource。要访问资源,我们可以使用@Value注释或ResourceLoader类。 @自动连线 私有资源加载器

@凌驾 公共无效运行(字符串…参数)引发异常{

    Resource res = resourceLoader.getResource("classpath:thermopylae.txt");

    Map<String, Integer> words =  countWords.getWordsCount(res);

    for (String key : words.keySet()) {

        System.out.println(key + ": " + words.get(key));
    }
}
Resource res=resourceLoader.getResource(“classpath:thermopylae.txt”);
Map words=countWords.getwordscont(res);
for(字符串关键字:words.keySet()){
System.out.println(key+”:“+words.get(key));
}
}

是否将此文件添加到web.xml或任何配置文件中,以告知应用程序它已存在。仅将文件添加到文件夹无助于应用程序扫描文件我没有web.xml,我有基于Java的配置。你能告诉我我应该考虑什么吗?也许你有样品?我会非常感激的!!!Luboskrnac为基于注释的解决方案提供了很好的推荐:非常感谢!!这真的救了我!现在它确实可以正常工作了。如何将该文件转换为JSonObejct?(首先数据是JSONobject)要添加到@luboskrnac的答案中,还可以尝试使用
File File=new ClassPathResource(“.\countries.xml”).getFile()
resource.getFile()
希望文件位于实际的文件系统上,而此解决方案在JAR中无法访问存储在
src/main/resources
下的资源。我已经用一个简单的Spring Boot应用程序确认了。@smeeb谢谢。使用
InputStream InputStream=newclasspathResource(“countries.xml”).getInputStream()是什么工作为我想你的答案!第一部分,正如你和卢博斯克纳克建议的那样,救了我@ElenaChubukina已经查看了示例。您说过您需要在启动时解析xml…谢谢!解析文件的部分没有问题(只有在类路径上查找文件的部分)-我使用CommandLineRunner,它可以很好地用于meIt works。还有一件事,请在springboot应用程序中使用依赖项:commons io commons io 2.6。旧的“toString”函数已弃用。您必须使用新版本:IOUtils.toString(inputStream,“UTF-8”)
    Resource res = resourceLoader.getResource("classpath:thermopylae.txt");

    Map<String, Integer> words =  countWords.getWordsCount(res);

    for (String key : words.keySet()) {

        System.out.println(key + ": " + words.get(key));
    }
}