Question

I want to load xml files which contains some error definitions of several modules in a Spring Maven project. I want to load and then pass the file to a JAXB unmasheller.

This is what I have done so far

String path = "classpath*:/**/definitions/error-definition.xml";
ClassPathResource resource = new ClassPathResource(path);
unmarshaller.unmarshall(resource);

My resource files are located as follows

src/main/resource/module1/definitions/error-definition.xml

src/main/resource/module2/definitions/error-definition.xml

This gives me the following error

java.io.FileNotFoundException: class path resource [classpath*:/**/definitions/error-definition.xml] cannot be resolved to URL because it does not exist

but when I change the path as follows

 String path = "/module1/definitions/error-definition.xml";

It works

Following are the other wild card which I tried with no luck

String paths = "classpath:/**/definitions/error-definition.xml";
String paths = "classpath*:error-definition.xml";
String paths = "classpath*:*.xml";

What I want to do is to use wild card to get the xml files from any folder under src/main/resource

I referred several previous SO answers but still couldn't figure out what Im doing wrong.

Was it helpful?

Solution

To load resource inject the ResourceLoader into your class. You can do this by either implementing ResourceLoaderAware or simply annotate a field of type ResourceLoader with @Autowired.

public class YourClass {

    @Autowired
    private ResourceLoader rl;

}

Now that you have the ResourceLoader you can use the ResourcePatternUtils to actually load the resources.

public Resource[] loadResources() {
    return ResourcePatternUtils.getResourcePatternResolver(rl).getResources("classpath:/directory/**/*-context.xml);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top