【问题标题】:Deploy Spring App to Tomcat without web.xml在没有 web.xml 的情况下将 Spring App 部署到 Tomcat
【发布时间】:2021-01-14 18:21:25
【问题描述】:

我已经使用嵌入式 TOMCAT 成功测试了我的 war 文件,它工作正常。现在我想将它部署到 TOMCAT,但我不断收到 404 错误。

我在浏览器上导航到的 URL 是 http://localhost:9999/springrest/demo/users/all

我的 TOMCAT 使用端口 9999

我的JRE_HOME版本是1.8.0_261

我的 pom.xml 有 1.8

TOMCAT 管理器显示应用程序正在运行

我的控制器代码如下

  @RestController
  @RequestMapping(path="/demo") 
  public class UserController {

  @Autowired 
  private UserRepository userRepository;

  @GetMapping(path="/users/all")
  public @ResponseBody Iterable<User> getAllUsers() {
  // This returns a JSON or XML with the users
  return userRepository.findAll();
  }

}

我的 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.0.0.RELEASE</version>
    <relativePath/> 
</parent>
<groupId>com.eptc</groupId>
<artifactId>springrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springrest</name>
<description>Rest API for mobile app</description>
<packaging>war</packaging>

<properties>
    <java.version>1.8</java.version>    
<start-class>com.eptc.springrest.SpringrestApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</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>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency> 
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>${artifactId}</finalName>
    <plugins>
    <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
</build>

【问题讨论】:

  • 为什么,具体来说,要部署在外部 Tomcat 中?
  • 我们有一台运行 LINUX CentOS 的服务器在工作。我必须使用该服务器,因为用于生产的 DATABASE 记录在上面。
  • 所以?您不需要外部 Tomcat;你可以使用java -jar。 Spring Boot 甚至支持创建独立的服务描述符,因此您可以从 init 运行它。 (虽然 Docker 对于这样的事情可能会更简单一些,而且在这样的 Linux 服务器上运行也很容易。)
  • @chrylis-cautiouslyoptimistic- 所以我只是将我的战争转储到我的 www 文件夹中,使用 java -jar 运行它并点击 www.myhostname.com/demo/users/all 在我的浏览器中?
  • 您是否阅读过 offical documentation 中的说明?还有您要部署到哪个 Tomcat 版本?

标签: java spring spring-boot tomcat http-status-code-404


【解决方案1】:

你是否通过实现 SpringBootServletInitializer 接口初始化了 Tomcat 所需的 Servlet 上下文?

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

详情请参考此链接:Deploy a Spring Boot WAR into a Tomcat Server

【讨论】:

  • 是的,我有。我还在 configure 方法 上做了 @Override
  • 我完全按照教程进行操作
  • @RynohRR 战争的名称是什么?是弹簧架吗?你试过localhost:9999/springrest/demo/users/all 吗?
  • 是的,它是弹簧架。从我得到的构建日志中......构建战争:C:\Users\similem\Documents\dev\Java\Spring\springrest\target\springrest.war
  • 你能分享你的pom文件吗?
【解决方案2】:

将我的 Apache Tomcat 版本 更改为 Apache Tomcat 8.5.58,它就像一个魅力。 Springboot 横幅甚至显示在 cmd 上运行 catalina.bat run

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多