Reputation: 2535
I'd like to take a xml file from my classpath to unmarshal it and use it for testing purposes. My problem is to get it as an InputStream. I wrote these lines but I always get a null result.
InputStream is = getClass().getResourceAsStream("WebContent/WEB-INF/classes/testing/"+ COMPLETE_DOCUMENT + ".xml");
of course the path you see in the method argument is the one to my file. I tried several combinations:
WebContent/WEB-INF/classes/testing/
classpath:testing/
classpath*:testing/
but I always get the InputStream = null.
I even tried to switch to
ClassLoader.getResourceAsStream(...)
but nothing happens. I suppose the path to the resource is somehow wrong, but I can't figure out where. From my servlet.xml I use some resource in the classpath configuring PropertyPlaceholderConfigurer or Jaxb2Marshaller just with the syntax
"classpath:folder/file.xsd"
and it works perfectly. The folder I want to load my xml from is a sibling of the one in the example above. What am I missing?
EDIT : I try to follow the spring ClassPathResource helper class approach and I get a strange behaviour: as I said before I already have some resources loaded from the classpath by some spring beans at the startup. If I use the path to such resources in the code suggested by dardo in as follows:
ClassPathResource cpr = new ClassPathResource("xmlschemas/lrinode.xsd");
InputStream is = cpr.getInputStream();
I Still get a FileNotFound Exception
!
Of course "xmlschemas/lrinode.xsd"
is a xsd I load at the startup successfully. It doesn't work even if I use the full path to the resource, starting from the root of the application.
I'm starting to think I'm missing something trivial.
Upvotes: 11
Views: 35674
Reputation: 4970
Spring provides a helper class named ClassPathResource
So something like:
ClassPathResource cpr = new ClassPathResource("folder/file.xsd");
InputStream is = cpr.getInputStream();
Should work, hope this helps!
Link to API Doc: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/core/io/ClassPathResource.html
Sidenote
Also, if you're using it for testing purposes, might want to wire a bean mapped to the xsd.
Might want to look into a JAXB marshaller
http://static.springsource.org/spring-ws/site/reference/html/oxm.html#oxm-jaxb2-xsd
Upvotes: 20
Reputation: 31795
When you call Class.getResourceAsStream()
with resource name without leading slash, it assumes you are requesting resource relative to a current package (i.e. package of the caller class). To make it an absolute resource path you need to add leading slash to resource name, e.g. in your case "/testing/"+ COMPLETE_DOCUMENT + ".xml"
Upvotes: 0
Reputation: 403601
You need a combination you didn't try:
getClass().getResourceAsStream("/testing/"+ COMPLETE_DOCUMENT + ".xml");
The WebContent/WEB-INF/classes
directory should already be on the classpath.
The classpath:
syntax only works if you're using Spring's ResourceLoader
abstraction, which you aren't. Your usage of classpath:folder/file.xsd
in your servlet.xml
woprks because Spring's passing it through a ServletContextResourceLoader
, which resolves classpath:
automatically.
Upvotes: 3