简体   繁体   English

Spring Boot war或jar for rest api project

[英]Spring Boot war or jar for rest api project

I want to develop sample REST API project using Spring Boot. 我想使用Spring Boot开发示例REST API项目。 I am confused what should be the proper approach as we have multiple options for packaging like war , jar etc. 我很困惑什么应该是正确的方法,因为我们有多种选择包装如warjar等。

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). 我有要求我有外部库文件夹,其中有多个jar和资源文件,将在REST API和前端(使用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. 由于动态变化,我希望将jar和资源保留为外部依赖项,我不想将它们包含在项目中。 I have tried sample project using loader.path using jar which works fine but same approach doesn't working with war file. 我已经尝试使用loader.path示例项目使用jar工作正常但相同的方法不使用war文件。 I am using Maven as build tool. 我使用Maven作为构建工具。

  1. What should be approach to achieve this in Spring Boot? 在Spring Boot中实现这一目的的方法是什么?
  2. Need working example in 2.xx version 需要2.xx版本的工作示例
  3. What should be used war or jar ? warjar应该用什么?
  4. How to configure IDE (Eclipse / IntelliJ) to use external lib folder with Spring Boot - I couldn't find solution for this. 如何配置IDE(Eclipse / IntelliJ)使用外部lib文件夹与Spring Boot - 我找不到解决方案。

You should make it an executable Spring Boot JAR. 你应该把它变成一个可执行的Spring Boot JAR。

You only need a WAR if you have to deploy it on a Java EE server. 如果必须在Java EE服务器上部署WAR,则只需要WAR。

It's good that you're using Maven. 你使用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. 您想要找到创建可执行JAR的Maven插件,其中包含依赖项。

Update: 更新:

Here are my responses to your four questions: 以下是我对您的四个问题的回答:

  1. Don't mix and match Maven and /lib. 不要混用和匹配Maven和/ lib。 Better to use mvn install to place all those external libraries you claim to need in your local .m2 or Maven repository. 最好使用mvn install将您声称需要的所有外部库放在本地.m2或Maven存储库中。
  2. See Spring Boot guides for working examples. 有关工作示例,请参阅Spring Boot指南 Perhaps the service and the React front end should be separate packages and deployments. 也许服务和React前端应该是单独的包和部署。
  3. This is Spring Boot, not Java EE. 这是Spring Boot,而不是Java EE。 Use an executable JAR, not a WAR. 使用可执行的JAR,而不是WAR。
  4. See suggestion 1. Install those JARs in Maven. 请参阅建议1.在Maven中安装这些JAR。 Do not mix and match. 不要混搭。

I'd recommend that you consider deploying the REST service separately and let the React front end call it. 我建议您考虑单独部署REST服务,让React前端调用它。 De-couple the two. 将两者分开。 Let the REST service be a microservice that stands on its own, without a UI. 让REST服务成为一个独立的微服务,没有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. 是否选择jar或war取决于您是否需要独立的可执行应用程序,或者您希望在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. 假设我的应用程序是复杂项目的中间层或适配器(帮助应用程序),我会将其作为战争部署在WebLogic上。

In your case My suggestion for you is to use a JAR instead of WAR. 在你的情况下我的建议是使用JAR而不是WAR。 To build jar use mvn clean install command. 要构建jar,请使用mvn clean install命令。

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: 此文件的applicationContext.xml文件条目如下:

<bean id="customResourceLoader" class="com.howtodoinjava.demo.CustomResourceLoader"></bean>

appendix- 附录-

You could create an executable JAR file with dependencies using Apache Maven Assembly Plugin . 您可以使用Apache Maven Assembly Plugin创建一个带有依赖项的可执行JAR文件。

<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. 将它添加到<build></build>元素中的pom.xml允许您构建jarjar-with-dependencies包。

To build package use mvn clean package command. 使用mvn clean package命令构建包。

  1. If you need a standalone app go for a jar. 如果你需要一个独立的应用程序去一个罐子。
  2. If you need to deploy in server go for war. 如果你需要在服务器上部署去战争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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