我正在尝试在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正在本地主机1234上运行。
我把war文件复制到了 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服务器上运行应用程序呢?
1条答案
按热度按时间atmip9wb1#
首先,您应该知道,默认情况下,SpringBoot提供了一个嵌入式ApacheTomcat构建,所以您不需要在ide中使用服务侧栏来提供它。之后,您需要确保项目结构遵循特定规则:
不要忘记,所有子包都应该位于groupid默认结构中。
此外,您还必须检查模板结构,确保设置正确,并且转发的资源确实存在于给定路径中。
最后,我真的建议你
<finalName>Your final name</finalName>
内<build>
加入pom.xml
要避免的文件/SpringbootTomcat-0.0.1-SNAPSHOT
神器铭文。