简体   繁体   中英

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.

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.

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-

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.

  1. If you need a standalone app go for a jar.
  2. If you need to deploy in server go for war.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM