将 Spring Boot WAR 部署到 Tomcat 8 - 访问资源时出现 HTTP 404

2023-12-19

我是 Spring Boot 的新手,正在努力将一个简单的 HTML Web 应用程序 (AngularJS) 部署到 Tomcat 8。这个 Web 应用程序仅提供一些 HTML/CSS/JS 内容,没有对后端的 REST 调用。它是使用 Webpack“编译”的——这会生成 JS/CSS 包和一个通过以下方式指向它们的 index.html 文件:<script> / <link>标签——并且已经在 ExpressJS 和带有嵌入式 tomcat 的 Spring Boot 上进行了测试,并且按预期工作。但沿着独立 WAR 的道路并部署到 Tomcat 8 似乎并不能正常工作。

对于 Spring Boot 项目,我已将所有 HTML/CSS/JS 文件包含在src/main/resources/public文件夹(无子文件夹)并且还配置了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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.my-app</groupId>
    <artifactId>my-app</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>

    <name>my-app</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <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.my.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class MyAppApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyAppApplication.class);
    }

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

以下是我用于进行 Tomcat 部署的步骤:

  1. mvn clean package生成 WAR
  2. 将WAR复制到path/to/tomcat8/webapp
  3. 启动Tomcat
  4. http://localhost:8080/my-app http://localhost:8080/my-app=> 自动加载index.html

不幸的是我看到的都是 404 错误,因为它找不到一些 JS/CSS 文件。我缺少什么配置吗?

Updated: 这是index.html 文件(通过Webpack 自动生成):

<!doctype html>
<html>
  <head>
    <base href="/">
    <meta charset="utf-8">
    <title>My App</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <link rel="icon" type="image/png" href="./assets/images/favicon.ico" />
  <link href="/main-aa49893f83cc830596563d81f09a9611.css" rel="stylesheet"><link href="/main-5949f1f257a55a77e48bc4ab62fbc99a.css" rel="stylesheet"></head>

  <body ng-app="myapp">
    <ui-view></ui-view>
  <script type="text/javascript" src="vendor-353ddb48f4ffe6546d59.js"></script><script type="text/javascript" src="app-353ddb48f4ffe6546d59.js"></script></body>
</html>

以下是我访问 Chrome Web Inspector 时看到的错误localhost:8080/my-app/index.html:

http://localhost:8080/main-5949f1f257a55a77e48bc4ab62fbc99a.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/main-aa49893f83cc830596563d81f09a9611.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/vendor-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/app-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/vendor-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/app-4ba9083ed9802279c207.js Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/main-aa49893f83cc830596563d81f09a9611.css Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8080/main-5949f1f257a55a77e48bc4ab62fbc99a.css Failed to load resource: the server responded with a status of 404 (Not Found)

还有一件事我忘了提。当我使用生成战争时mvn clean package, src/main/resources/public 下的所有文件都放入WEB-INF/classes/resource子文件夹。根据研究,这些文件不是公开可见的(即,如果我尝试访问 localhost:8080/my-app/foo.css,它将给出 404)。这就是index.html文件无法“看到”它所依赖的JS/CSS文件的原因吗?


我最终弄清楚了,但没有使用 Spring Boot 来打包 WAR 文件。我当地还有另一个项目,它使用普通的旧版本Maven + pom.xml + web.xml创建 WAR,我用它作为参考来找出当前项目无法运行的原因。有多个问题:

  • 当您使用默认配置部署到 Tomcat 8 时,它会将 WAR 文件的名称(他们称为上下文)附加到其路径中。在这种情况下,它是http://localhost:8080/my-app。 “编译”的 AngularJS 应用程序index.html had a <base href="/">需要指向的标签/my-app/代替/。这是 JS/CSS 文件在中不可见的主要原因Web Inspector > Sources.
  • <link> tag's src属性不应包含前导/
  • 对于我上面发布的 Spring Boot 应用程序,它附带嵌入式 Tomcat 并将应用程序部署在根上下文中,因此无需更改 index.html 文件中的任何路径。
  • 与 Spring Boot 类似,我在 ExpressJS 中运行应用程序也没有出现任何问题,因为没有创建“子上下文”。同样,在这种情况下不需要修改任何文件。

还有与查找资源相关的其他错误,例如.ttf文件,但至少应用程序能够运行。

更新: 看起来可以通过在靠近底部的位置添加以下内容来从 Tomcat 8 根目录提供 WAR 文件server.xml file:

<Context path="" docBase="my-app" debug="0" reloadable="true"></Context>

这将避免修改index.html 文件的需要。

惊人的 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 Spring Boot WAR 部署到 Tomcat 8 - 访问资源时出现 HTTP 404 的相关文章

随机推荐

  • 使用 UINavigationBar 和 UIBarButtonItem 自动布局

    我喜欢将视图创建为独立的 Xib 文件 然后实例化它们并添加为子视图 所以 当与一个UINavigationBar 我希望能够做同样的事情 首先从 Xib 创建我的自定义视图 然后将其作为自定义视图添加到UIBarButtonItem UI
  • 如何在 Django Admin 中使用“list_display”显示多对多字段?

    class Product models Model products models CharField max length 256 def unicode self return self products class Purchase
  • 将 opencover xml 输出转换为 ncover xml

    我需要能够将 opencover 覆盖率结果导入 Jenkins 以随时间推移提取覆盖率趋势数据 看来 最好的方法是找到一种方法将 opencover xml 格式转换为 ncover 格式 然后使用 NCover 插件导入结果 是否有现有
  • 将未绑定的形式转换为绑定的形式?

    我想要一个对象的绑定形式来使用 is valid 方法 原因是因为我有一些旧数据 我希望用户根据新的验证规则进行更正 然后 我想在我的表单中重用干净方法的代码 我最终序列化了我的回复 from django utils import sim
  • 使用新文档支持 API 进行 iOS 应用程序到应用程序的数据传输

    Problem Building Enterprise Applications of a Suite Nature and need to be able to pass data from one application to anot
  • mingw32-make是否改名为make?

    我安装了最新的mingw 发现已经没有mingw32 make了 有make exe 所以我想知道最近是否将mingw32 make重命名为make exe 我不知道你从哪里得到 MinGW 但一些发行版 比如 nuwen 的 命名了它ma
  • 如何将jar上传到存储库?

    我的 jar 文件在 Maven 中央存储库中找不到 我想添加罐子 这样我就可以在我的pom xml文件和其他开发人员可以使用该 jar 将 jar 上传到 http web 服务器 web 文件夹需要执行哪些步骤 我还应该上传什么文件cu
  • Grails在tomcat下爆炸

    有没有一种方法可以在 tomcat 上以 爆炸 模式使用 Grails 以便在正在运行的应用程序 如 gif 中进行单独更改 而无需重新生成整个 war 并上传它 比如在 tomcat 的conf Catalina localhost 中添
  • 如何从 C# 字符串中获取以 null 结尾的字符串?

    我正在与需要空终止字符串的服务器通信 我怎样才能做到这一点smartly in C 我认为聪明的方法就是简单地做 string str An example string char MinValue Add null terminator
  • 在 HttpClient 中打开日志记录

    如何正确打开 Apache Commons HttpClient 的日志记录 现在我正在这样做 除了我作为测试明确触发的消息之外 没有收到任何日志消息 public class HttpTest1 static Log log LogFac
  • 将 Scala 文件加载到解释器中以使用函数?

    我在文件中而不是在类中定义了一些 Scala 函数 并且我想在 Scala 解释器中使用它们 我知道我可以说scala filename scala只需运行该文件并退出解释器 但我想运行该文件 然后留在解释器中 以便我可以进行一些测试 谁能
  • 使用 markdown 创建 元素 (kramdown)

    是否可以使用 markdown 创建一个 span 元素 我正在使用 Kramdown 转换器 Thanks 根据Markdown 语法规范 文档 http daringfireball net projects markdown synt
  • 将 scala.math.BigDecimal 转换为 java.math.BigDecimal?

    我如何转换scala math BigDecimal to java math BigDecimal 无需与字符串进行双重转换 val sb scala math BigDecimal 12345 val jb sb bigDecimal
  • 在数据表的分页控件中显示不带省略号的页面

    我正在使用 jQuery Datatables JS 和 Bootstrap 我遇到了一个问题 有一个解决方法 但它不是最好的 问题 我的表包含超过 4k 条记录 如果处理表的人需要从记录 200 到 300 进行处理 并且为了方便起见 他
  • PHP - 从数据库构建多级关联数组(从数据库按州对城市进行排序)

    我对 php 有点陌生 在过去的几个小时里我一直在绞尽脑汁试图弄清楚这一点 我需要从数据库中按州对城市进行排序 我使用以下查询来检索数据集 SELECT state city FROM table ORDER BY state ASC ci
  • 如何克隆道具对象并使其不反应[重复]

    这个问题在这里已经有答案了 我有一些表单数据 我通过道具与子组件共享 现在我想克隆 prop 对象并使其不响应 就我而言 我希望用户能够修改 props 值 而无需实际更改克隆值 克隆值应该仅用于向用户显示编辑时的表单内容 下面的代码显示了
  • Swift:.classForCoder() 的替代方案

    给出以下代码 return TyphoonDefinition withClass AppDelegate classForCoder definition in definition injectProperty assembly 有必要
  • 内核sys_call_table地址与system.map中指定的地址不匹配

    我正在尝试温习 C 所以我一直在研究 Linux 内核的系统调用表 在 3 13 0 32 generic 上 我在网上找到了一个资源 它使用以下函数搜索系统调用表 并将其加载到 LKM 中的内核中 static uint64 t aqui
  • 如何在 Linq 中进行完全外连接?

    我继承了一个设计不完全优化的数据库 并且我需要操作一些数据 让我对我必须做的事情给出一个更常见的类比 假设我们有一个Student桌子 一个StudentClass记录他参加的所有课程的表格 以及StudentTeacher表存储了所有教过
  • 将 Spring Boot WAR 部署到 Tomcat 8 - 访问资源时出现 HTTP 404

    我是 Spring Boot 的新手 正在努力将一个简单的 HTML Web 应用程序 AngularJS 部署到 Tomcat 8 这个 Web 应用程序仅提供一些 HTML CSS JS 内容 没有对后端的 REST 调用 它是使用 W