我一直在尝试创建一个 WAR 文件并将其部署在 Tomcat 服务器上。如果我在 Tomcat 服务器上运行 IntelliJ 内的 WAR 文件,它就可以工作。
为了使事情尽可能简单,我遵循了本教程(https://www.baeldung.com/spring-boot-war-tomcat-deploy),甚至那个也行不通。部署 war 文件时,我仍然遇到相同的 404 错误。教程应用程序在 IntelliJ 中工作,就像我的主要项目一样。我还很新,尝试了很多东西都无济于事:
尝试在 pom.xml 文件中设置 line war,清理 maven 并使用目标文件夹中生成的 war 文件进行 Tomcat 服务器部署,或者在官方网站(https://www.jetbrains. com/help/idea/deploying-a-web-app-into-an-app-server-container.html)并尝试运行它。
尝试了不同的 URL,但它们都抛出 404 错误(除了默认的 Tomcat 页面,如 /manager)
在 application.properties 文件中设置不同的路径 (server.servlet.context-path=/test)
试过不同的电脑
错误:
我可能会犯什么新手错误?
我的pom:
<?xml version="1.0" encoding="UTF-8"?>
https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE com.example tomcat-test2 0.0.1 -SNAPSHOT war tomcat-test2 Spring Boot 演示项目
<properties>
<java.version>1.8</java.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我的控制器:
@RestController
公共类 TomcatController {
@GetMapping("/hello")
public Collection<String> sayHello() {
return IntStream.range(0, 10)
.mapToObj(i -> "Hello number " + i)
.collect(Collectors.toList());
}
}
主要应用:
@SpringBootApplication 公共类 TomcatTest2Application 扩展 SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TomcatTest2Application.class, args);
}
}
提前致谢, 亚历克斯