【发布时间】: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