繁体   English   中英

Java Spring Boot - 未在 Tomcat Apache 上部署的战争文件

[英]Java Spring Boot - war file not deploying on Tomcat Apache

我正在构建一个 Java Spring 启动项目 - 作为一项要求,我需要将该项目生成为一个 war 文件 - 然后让 Tomcat Apache 单独运行。

我已经构建了 war 文件,但是当我尝试部署它时,它好像结构不正确并且它生成 404 试图查看项目。

  • 问题在此处输入图片说明

在此处输入图片说明 在此处输入图片说明

  • 我的绒球

    http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

     <groupId>org.springframework</groupId> <artifactId>gs-spring-boot</artifactId> <version>0.1.0</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api --> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <!-- mysql connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mail service --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- hot swapping, disable cache for template, enable live reload --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>provided</scope> </dependency> <!-- freemarker template --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <!-- json simple --> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> <!-- tag::actuator[] --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- end::actuator[] --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.194</version> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

要将 Spring Boot 部署为战争 - 您需要重新配置 Application 类。

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-create-a-deployable-war-file

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

在 pom.xml 添加

<packaging>war</packaging>

当我使用 maven 时,我还添加了依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

我的应用程序很复杂,因为我使用 reactjs 作为前端表示层,而使用 Java 作为 api 层。 我不得不将 reactjs 构建移动到 main/src/webapps 中——然后它会在 war 文件中正确呈现为 js、css 和 html 文件包。 使用 reactjs 作为表示层,它只能通过 api 连接到 java 端——所以 url 结构很重要,我已经把所有东西都放在了预期中,它会从根运行。 因此,如果您部署到 tomcat - 您可能需要重命名战争文件 ROOT.war。

Java Spring Boot - 未在 Tomcat Apache 上部署的战争文件

Spring-Boot 与 application.jar 一起构建,其中包括应用程序服务器。

Spring Boot旨在使创建由 Spring 驱动的生产级应用程序和服务变得容易,而且不会大惊小怪。 它对 Spring 平台采取了固执的观点,以便新用户和现有用户可以快速获得他们需要的信息。 您可以使用它来创建可以使用“java -jar”或更传统的 WAR 部署启动的独立 Java 应用程序。 我们还提供了一个运行“spring 脚本”的命令行工具。

https://spring.io/blog/2013/08/06/spring-boot-simplifying-spring-for-everyone

因此,您无需再次部署在 Apache Tomcat 应用程序服务器中。 就像简单的 java .jar 文件一样运行它。

nohup java -jar app.jar &

Spring Boot Web 项目Spring Boot WebApp 作为 WAR 的一个工作示例

通过 maven 构建的常规 SpringBoot 应用程序是一个打包的 jar。

您需要将其修改为 WAR 并检查 WEB-INF 的内容是否正确。

打包成war的Spring Boot应用不是为了单独部署到Tomcat中,而是直接通过Spring Boot App(通过main方法)运行。 您需要进行测试,但根据我的理解,如果有人建议您将其部署在 tomcat 中,那么您需要通知他们他们要求的是常规 Web 应用程序而不是 SpringBoot 应用程序。

每个 SpringBoot 应用程序运行自己的容器。

此外,您将视图文件保存在模板目录中,如果您不使用 Thymeleaf,则无法从模板目录中将它们作为静态内容提取。

根据传统部署页面,您必须添加依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

暂无
暂无

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

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