Spring boot 3.0.1应用程序未在tomcat9外部服务器上运行

问题描述 投票:0回答:1

当使用我的Eclipse的Tomcat9服务器(v9.0.62)时,我的“应用程序”运行正常。 然而,一旦部署到我的生产服务器上(v9.0.43),就会返回 404。 Tomcat 没有记录任何堆栈跟踪。

两台服务器都使用 OpenJDK17 作为 JDK。 使用 Spring 版本时不会出现此问题< 6.

Eclipse 控制台输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.1)

2023-01-05T11:07:33.198+01:00  INFO 28395 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 17.0.5 with PID 28395 (/home/-----/demo/target/classes started by ----- in /home/-----/demo)
2023-01-05T11:07:33.207+01:00  INFO 28395 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2023-01-05T11:07:34.391+01:00  INFO 28395 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2023-01-05T11:07:34.403+01:00  INFO 28395 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-05T11:07:34.404+01:00  INFO 28395 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-05T11:07:34.530+01:00  INFO 28395 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-05T11:07:34.532+01:00  INFO 28395 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1263 ms
2023-01-05T11:07:34.889+01:00  INFO 28395 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2023-01-05T11:07:34.895+01:00  INFO 28395 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.316 seconds (process running for 2.652)
2023-01-05T11:07:47.016+01:00  INFO 28395 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-01-05T11:07:47.016+01:00  INFO 28395 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-01-05T11:07:47.017+01:00  INFO 28395 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

构建(maven clean install)war 文件时,仍然没有问题:

[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ demo ---
[INFO] Deleting /home/-----/demo/target
[INFO] 
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ demo ---
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/-----/demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ demo ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ demo ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:3.3.2:war (default-war) @ demo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo] in [/home/-----/demo/target/demo-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/-----/demo/src/main/webapp]
[INFO] Building war: /home/-----/demo/target/demo-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:3.0.1:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- maven-install-plugin:3.0.1:install (default-install) @ demo ---
[INFO] Installing /home/-----/demo/pom.xml to /home/--/.m2/repository/com/example/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.pom
[INFO] Installing /home/-----/demo/target/demo-0.0.1-SNAPSHOT.war to /home/--/.m2/repository/com/example/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.232 s
[INFO] Finished at: 2023-01-05T11:09:35+01:00
[INFO] ------------------------------------------------------------------------

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>3.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
<!--        <tomcat.version>9.0.62</tomcat.version>-->
    </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>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

应用:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

Servlet初始化器:

package com.example.demo;

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(DemoApplication.class);
    }

}

控制器:

package com.example.demo.ctrl;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {

    @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE)
    public @ResponseBody String getMain() {
        return "super";
    }
}

预期(Eclipse 的服务器):

找到(生产服务器):

为了查明问题(因为我的项目相当大),我使用一个基于 Spring Initializr 的简单应用程序重现了该问题。

我尝试调整 pom.xml 但没有成功。

java spring tomcat spring-boot-3
1个回答
7
投票

Spring boot 3 至少需要 JDK17 和 Tomcat 10,因为包从 javax 重命名为 jakarta。

Tomcat 9 不适用于 jakarta 软件包,这是 Spring 6/Spring boot 3 中引入的更改。

如果您想继续使用 Tomcat 9,则需要降级到 Spring boot 版本 2.x.x,任何 Spring boot 3 版本都需要将 Tomcat 升级到版本 10。

© www.soinside.com 2019 - 2024. All rights reserved.