Spring-Boot Resource Not Found when using executeable Jar

后端 未结 3 1076
情话喂你
情话喂你 2020-12-19 09:21

again I face a strange issue and hope someone here can help.

I have a spring boot backend module, what works in eclipse well and application is executeable when sta

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 10:00

    Now I needed to find xmlFiles into a resource folder from a JAR and facing similar problems described here already. I would like to share my findings and how I got it work, maybe it is helpful.

    In a jar I have "db-input" folder under src/main/resources with any number for xml-Files for DB-Input. My application is Spring-Based:

    @Component
    public class DatabaseInitializer implements InitializingBean {
        @Autowired DomainConfigurationRepository repository;
        @Autowired MarshallerService marshallerService;
        @Autowired ApplicationContext context;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            final Resource[] resources = context.getResources("classpath*:db-input/*");
            final Set filePaths = findInputFileNames(resources);
            final Set configurations = createConfigurations(filePaths);
            repository.save(configurations);
        }
    
        private  Set createConfigurations(final Set filePaths) throws Exception {
            final Set configurations = new HashSet<>();
            for(final String path : filePaths){
                final Resource resource = context.getResource(path);
                final DomainConfigurationXO xoConfiguration = marshallerService.unmarshal(resource.getInputStream());
                final DomainConfiguration configuration = PopulationUtils.getPopulatedConfiguration(xoConfiguration);
                configurations.add(configuration);
            }
            return configurations;
        }
    
        public Set findInputFileNames(final Resource[] inputDirectoryResources) throws IOException {
            return Arrays.stream(inputDirectoryResources)
                    .map(resource -> extractURI(resource))
                    .collect(Collectors.toSet());
        }
    
        private String extractURI(Resource resource){
            try {
                return resource.getURI().toString();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    

提交回复
热议问题