Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load a (xml) file from the classpath in a spring web app

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.

like image 948
MaVVamaldo Avatar asked Mar 02 '12 16:03

MaVVamaldo


People also ask

What is classpath in Spring XML?

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader. getResources(...) call), and then merged to form the final application context definition. So classpath: starts at the root of your classpath.

How does classpath work in Spring?

It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable . jar , some physical file system location used in the classpath (with java 's -cp option), etc.

How do I read a classpath file?

To read any file from the classpath in a class, we have to get the reference of the system classloader for that class that is trying to read the file. System classloader obviously knows the other paths for the application. Once we have the File reference, we can use a number of ways to read the file.

What is used to load the bean configuration file located at the specified class path?

ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext. xml"); The bean definitions will be loaded from the classpath, as a ClassPathResource will be used.


2 Answers

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

like image 123
dardo Avatar answered Oct 02 '22 16:10

dardo


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.

like image 32
skaffman Avatar answered Oct 02 '22 15:10

skaffman