gpt4 book ai didi

java - 使用 Spring Boot 读取资源文件夹中可用的文件

转载 作者:行者123 更新时间:2023-11-30 06:47:05 25 4
gpt4 key购买 nike

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

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();

谁能帮我解决这个问题

enter image description here

最佳答案

你可以使用 ClassPathResource

像这样:

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

或者

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

关于java - 使用 Spring Boot 读取资源文件夹中可用的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46563392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com