Spring MVC : read file from src/main/resources
I have a maven Spring project, there is xml
file inside src/main/resources/xyz.xml
. How can I read it inside spring MVC controller.
I am using
InputStream is = getClass().getResourceAsStream("classpath:xyz.xml");
but is
is null
.
Resource resource = new ClassPathResource(fileLocationInClasspath);
InputStream resourceInputStream = resource.getInputStream();
using ClassPathResource and interface resource. But make sure you are copying the resources directory correctly (using maven), and its not missing, for example if running tests as part of test context.
For spring based application you can take advantage of ResourceUtils class.
File file = ResourceUtils.getFile("classpath:xyz.xml")