2017-10-04 1 views
1

Springbootアプリケーションのresourcesフォルダにあるファイルを読み込みます。私はそれを行うためにResourceLoaderを使用しました。しかし、アプリケーションを実行しようとするとFileNotFoundExceptionが発生します。以下はSpringBootでリソースフォルダにあるファイルを読み込む

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

+0

@Yannic Klem NO ...これはあなたが言及した質問の重複ではありません。 ClassLoaderでの使用はここでは機能しません。ここで私はResourceLoaderを使ってファイルにアクセスする方法を尋ねています。これら2つはどうやって似ていますか?内容が無駄になる前に内容を読むようにしてください。 – user5489038

+0

あなたは正しいです。別の質問を複製します:https://stackoverflow.com/questions/41754712/spring-boot-reading-text-file-using-resourceloader試してください役に立たない質問を投稿する前にグーグルに1回だけ。 –

+0

'jar'からファイルを取得することはできません..ストリームとしてファイルを取得してみてください。 – Jaiwo99

答えて

0

これを整理するために私を助けてくださいすることができ、スプリングのフォルダressources内のファイルにアクセスするには、以下のコードを使用しようブート

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

あなたはこのようClassPathResource

SMTHを使用することができます。

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

それとも

File file = new ClassPathResource("schema.graphql").getFile(); 
関連する問題