Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot war or jar for rest api project

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.

  1. What should be approach to achieve this in Spring Boot?
  2. Need working example in 2.xx version
  3. What should be used war or jar?
  4. How to configure IDE (Eclipse / IntelliJ) to use external lib folder with Spring Boot - I couldn't find solution for this.
like image 403
Karim Narsindani Avatar asked Aug 02 '18 12:08

Karim Narsindani


People also ask

Does spring boot create jar or war?

The spring-boot-loader modules lets Spring Boot support executable jar and war files. If you use the Maven plugin or the Gradle plugin, executable jars are automatically generated, and you generally do not need to know the details of how they work.

Is jar or war better?

JAR files allow us to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications. The structure of the archives is also different. We can create a JAR with any desired structure.

Is spring boot GOOD FOR REST API?

Advantages of using Spring Boot A few benefits of using Spring Boot for your REST APIs include: No requirement for complex XML configurations. Embedded Tomcat server to run Spring Boot applications. An auto-configuration feature by Spring Boot that configures your application automatically for certain dependencies.


4 Answers

You should make it an executable Spring Boot JAR.

You only need a WAR if you have to deploy it on a Java EE server.

It's good that you're using Maven. Have it manage your dependencies and build the package.

You want to find the Maven plug-in that creates the executable JAR with dependencies included inside.

Update:

Here are my responses to your four questions:

  1. Don't mix and match Maven and /lib. Better to use mvn install to place all those external libraries you claim to need in your local .m2 or Maven repository.
  2. See Spring Boot guides for working examples. Perhaps the service and the React front end should be separate packages and deployments.
  3. This is Spring Boot, not Java EE. Use an executable JAR, not a WAR.
  4. See suggestion 1. Install those JARs in Maven. Do not mix and match.

I'd recommend that you consider deploying the REST service separately and let the React front end call it. De-couple the two. Let the REST service be a microservice that stands on its own, without a UI.

like image 152
duffymo Avatar answered Oct 13 '22 14:10

duffymo


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-

  • http://www.java2novice.com/spring-boot/load-external-configuration-files/
  • https://howtodoinjava.com/spring/spring-core/how-to-load-external-resources-files-into-spring-context/
like image 26
Shubham Chopra Avatar answered Oct 13 '22 13:10

Shubham Chopra


  1. If you need a standalone app go for a jar.
  2. If you need to deploy in server go for war.
like image 5
Ashok Kumar N Avatar answered Oct 13 '22 13:10

Ashok Kumar N


You could create an executable JAR file with dependencies using Apache Maven Assembly Plugin.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>${mainClass}</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Adding it to your pom.xml within <build></build> elements allows you to build both jar and jar-with-dependencies packages.

To build package use mvn clean package command.

like image 1
DimaSan Avatar answered Oct 13 '22 12:10

DimaSan