How to read the Json file from Resources - java

I am developping Spring Java application with Maven. I need to read a .json file, that is situated in Resources folder in my project, and than return it by Controller again as a .json file. How can I do that? I am a beginner...

Resourece folder is in your class path. Any file which is in your class path , you can read through this.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourFile.json");
you can read by using getResourceAsStream.

If you access to file in Spring bean you could also use #Value annotation like that (It work only in spring beans)
#Value("classpath:data/resource-data.txt")
Resource resourceFile;
https://www.baeldung.com/spring-classpath-file-access#2-using-value

Related

How to use Resource[] in spring to read files from windows file system specified in application.properties

I'm trying to use spring's Resource interface to read files from local machine using spring boot project. I specified the file path in application.properties
filePath=c/tmp/*.pdf
and trying to read using #value annotation in the code.
#Value("${filePath:}")
private Resource[] filePath;
However when I try to run the app its giving me the following error
cannot be resolved to URL because it does not exist
Can someone help me understand on how to properly read files from external folder which is not in a classpath and specified in application.properties file?

How to Create a Spring bean that accesses files outside of my Project Folder?

Can FileReadingMessageSource in Spring be used to create a Spring bean which can access files like a config files?
I am trying to access this:
-my app
C:\folder1\eclipseapp.jar
-config file
C:\folder1\config.properties
Please teach me to create Spring bean to access my "config.properties" file located outside my project folder.
Thanks

Load resource in a jar file Java Spring

I want to load a html file located in:
to use in standalone spring boot jar application.
This approach leads to FileSystemNotFoundException
new InputStreamReader(
getClass().getResourceAsStream("/email-templates/html-email.html")
)
Using
#Autowired
private ResourceLoader resourceLoader;
...
resourceLoader.getResource("classpath:email-templates/html-email.html");
leads to NullPointerException.
Could you please specify how to properly load a file in spring boot jar.
Try below code. Using spring classpathresource you should be able to load them. As long as you try load this file inside spring context, Spring container is aware of its classpath .So it should load corresponding file.Otherwise , it tries to find in file system.
Resource resource = new ClassPathResource("email-templates/html-email.html");
I assume you have these resouces in your jar, Check inside the jar file if those resources are present.
jar tf springboot.jar
Load resource in a jar file Java Spring
Try through below code.
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("email-templates/html-email.html");
/email-templates/html-email.html
If the above doesn't work visit this help full link
Java ClassLoaderUtil

Spring Boot - Identify resources under resource folder

I have a Spring Boot App. I am trying to read few files that I have placed under main/resources folder. I see that Spring Boot automatically reads application.properties under resources folder. However it doesn't seem to read other xml / text files that I have placed under resources folder.
Source xslt = new StreamSource(new File("removeNs.xslt"));
Should I add any additional configuration for the program to automatically read all the files under resources folder without having to explicitly provide the path?
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("removeNs.xslt").getFile());
This should work.
Thanks #Edwin for mentioning a more robust solution:
File file = new File(Thread.currentThread().getContextClassLoader().getResource("removeNs.xslt").getFile());
You need to specify that you want to read specific files under resources folder using
#PropertySource annotation.
you can specify multiple property sources using
#PropertySources({
#PropertySource(),
#PropertySource()
})

Reading a XML file from JAR within web application

I am trying to read XML file from JAR used as library in my web app. How can I achieve that? My directory structure for XML file is:
commons/resources/config.xml
where commons is a Java project for which I will create a JAR and place under
apache-tomcat-8.0.32/webapps/myWebApp-1.4/WEB-INF/lib
after building and deploying it.
You can read the XML file as resource from classpath this way: this.getClass().getResourceAsStream("/config.xml"). It returns a stream that you can use for reading the content of the file and do with it whatever you want. It is only possible within the webapp the jar belongs to because webapps are isolated with respect to classloading.

Categories