使用 SpringBoot 读取资源文件夹中可用的文件 [英] Reading file available in resources folder with SpringBoot

查看:98
本文介绍了使用 SpringBoot 读取资源文件夹中可用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将读取 Springboot 应用程序中资源文件夹中可用的文件.我已经使用 ResourceLoader 来做到这一点.但是当我尝试执行我的应用程序时,我收到了 FileNotFoundException.

I am going to read a file available in resources folder in my Springboot application. I have used ResourceLoader to do it. But I get a FileNotFoundException when I try to execute my application.

Error creating bean with name 'demoController': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [schema.graphql] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/dilan/Projects/demo/target/demo.jar!/BOOT-INF/classes!/schema.graphql
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]

下面是我的代码

 @Autowired
private ResourceLoader resourceLoader;

final Resource fileResource = resourceLoader.getResource("classpath:schema.graphql");
File schemaFile = fileResource.getFile();
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();

谁能帮我解决这个问题

在此处输入图片描述

推荐答案

你可以使用 ClassPathResource

像这样:

InputStream inputStream = new ClassPathResource("schema.graphql").getInputStream();

或者

File file = new ClassPathResource("schema.graphql").getFile();

这篇关于使用 SpringBoot 读取资源文件夹中可用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆