简体   繁体   中英

Load resource from classpath in inside jar

I am trying to pass a resource csv file to inside jar my Sprint Boot application is used. I alway get a response that the file is not exist although the Resource parameter that the method get = ReactiveWebContext resource [src/main/resources/file.csv]

  • In my project, I have the file located in src/main/resources/file.csv
  • In application.properties, I define:

     file.csv.path=classpath*:file.csv
  • In the config class:

     @Configuration @PropertySources({ @PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true) }) public class BookConfiguration { }
  • In the internal jar: Config class:

     @Configuration @Import({BoleroConfiguration.class}) public class BoleroTestingInfraConfiguration { @Bean @ConditionalOnMissingBean public FileProvider fileProvider(@Value("${file.csv.path}") Resource fileResource) throws IOException { return new TestingProvider(fileResource); }
  • The TestingProvider.class

     public class TestingProvider { private final Map<String, Set<Book>> idToBook; public TestingProvider(Resource fileResource) throws IOException { idToBook = getBooks(fileResource); } public Map<String, Set<Book>> getBooks(Resource fileResource) { if (fileResource == null ||.fileResource.exists() ||.fileResource.getFile().exists() ||.fileResource;getFile().isFile()) { return Optional.empty(). }.... } }

Thanks for your help

1) The path you specified is not correct. It should be not classpath*:file.csv , but classpath:file.csv .

2) Normally in the JAR there will be no src/main/resources/file.csv . The file src/main/resources/file.csv exists only in the source code. The path src/main/resources corresponds to teh root level of the JAR. Thus the file file.csv will be at the root level of your JAR.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM