【问题标题】:How to run Springboot app on Tomcat server?如何在 Tomcat 服务器上运行 Spring Boot 应用程序?
【发布时间】:2021-08-20 16:39:18
【问题描述】:

我正在尝试在 Tomcat 服务器上运行 Springboot 应用程序,但是我的所有实验都失败了。

在这里你可以看到pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringbootTomcat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>SpringbootTomcat</name>
    <description>Demo project for Spring Boot with Tomcat</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>com.example.SpringbootTomcat.SpringbootTomcatApplication</start-class>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主类:

package com.example.SpringbootTomcat;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringbootTomcatApplication {

//  @GetMapping("/test")
    @RequestMapping(value = "/test")
    public String test(){
        return "Application deployed!";
    }

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

}

ServletInitializer 在另一个类中:

package com.example.SpringbootTomcat;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}

它是从https://start.spring.io/ 生成的。

Tomcat 正在 localhost:1234 上运行。

我将战争文件复制到webapps 文件夹中。但是,当我在浏览器中输入 http://localhost:1234/SpringbootTomcat-0.0.1-SNAPSHOT/test 时,我收到以下消息:

HTTP Status 404 – Not Found
Type Status Report

Message The requested resource [/SpringbootTomcat-0.0.1-SNAPSHOT/test] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

我在我的电脑上运行 Tomcat 10.0.6,CATALINA_HOME 设置为 Tomcat 的文件夹,CLASSPATH 是CATALINA_HOME\lib。

它只能用作 Maven 应用程序,但不能用作 Tomcat 应用程序。

如何在 Tomcat 服务器上运行应用程序?

【问题讨论】:

  • 发布tomcat服务器启动日志
  • 我不认为 Spring Boot 与 Tomcat 10 兼容。尝试 Tomcat 9。
  • Tomcat 10 不受支持,因为它是 JakartaEE 实现。使用 JEE 实现的 Tomcat 9。

标签: java spring spring-boot maven tomcat


【解决方案1】:

首先,您应该知道,默认情况下,Spring Boot 提供嵌入式 Apache Tomcat 构建,因此您无需使用服务侧边栏在 IDE 中提供它。之后你需要确保你的项目结构遵循一定的规则:

  1. 不要忘记所有子包都应该位于你的 groupId 默认结构中。

  2. 您还必须检查模板结构并确保设置正确,并且转发的资源确实存在于给定路径中。

  3. 最后,我真的建议您在pom.xml 文件中的&lt;build&gt; 标签内设置&lt;finalName&gt;Your final name&lt;/finalName&gt;,以避免/SpringbootTomcat-0.0.1-SNAPSHOT 标记工件。

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 2017-12-15
    • 2017-07-17
    • 2020-11-27
    • 2018-05-13
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    相关资源
    最近更新 更多