I want to develop sample REST API project using Spring Boot. I am confused what should be the proper approach as we have multiple options for packaging like war
, jar
etc.
I have requirement where I have external library folder which have multiple jar
and resource files which will be used in REST API and front end (using React).
I want to keep jars and resource as external dependencies due to their dynamic changes and I do not want to include them in project. I have tried sample project using loader.path
using jar
which works fine but same approach doesn't working with war
file. I am using Maven as build tool.
- What should be approach to achieve this in Spring Boot?
- Need working example in 2.xx version
- What should be used
war
orjar
? - How to configure IDE (Eclipse / IntelliJ) to use external
lib
folder with Spring Boot - I couldn't find solution for this.
Whether to choose jar or war depends upon whether you want a standalone executable application or you want to deploy your project on servers like Weblogic. Suppose if my application is a middle layer or an adaptor(helper application) of a complex project I would deploy it on WebLogic as war.
In your case My suggestion for you is to use a JAR instead of WAR. To build jar use mvn clean install command.
In order to load external properties file all you need to do is pass folder names and property names as part of command line arguments as shown below:
java -jar myapp.jar --spring.config.name=application,myapp
-- spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config
In order to externally import Resources, you can use
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");
code snippet
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class CustomResourceLoader implements ResourceLoaderAware
{
private ResourceLoader resourceLoader;
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void showResourceData() throws IOException
{
//This line will be changed for all versions of other examples
Resource banner = resourceLoader.getResource("file:c:/temp/filesystemdata.txt");
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true) {
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
reader.close();
}
}
And applicationContext.xml file entry for this file is as below:
<bean id="customResourceLoader" class="com.howtodoinjava.demo.CustomResourceLoader"></bean>
appendix-