访问资源时将Spring Boot WAR部署到Tomcat 8-HTTP 404 [英] Deploying Spring Boot WAR to Tomcat 8 - HTTP 404 when accessing resources

查看:101
本文介绍了访问资源时将Spring Boot WAR部署到Tomcat 8-HTTP 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Spring Boot and struggling to deploy a simple HTML web app (AngularJS) to Tomcat 8. This web app simply serves some HTML/CSS/JS content with no REST calls to a backend. It has been "compiled" using Webpack -- this produces JS/CSS bundles and a single index.html file that points to them via <script> / <link> tags -- and has been tested on ExpressJS and Spring Boot w/ Embedded tomcat and works as expected. But going down the path of a stand-alone WAR and deploying to Tomcat 8 does not seem to work properly.

对于Spring Boot项目,我已经将所有HTML/CSS/JS文件包含在src/main/resources/public文件夹中(没有子文件夹),并且还对pom.xml进行了如下配置(基本配置):

For the Spring Boot project, I've included all the HTML/CSS/JS files in the src/main/resources/public folder (no subfolders) and have also configured the pom.xml as follows (basic config):

<?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部署的步骤:

Here are the steps I used to do the Tomcat deployment:

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

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

Unfortunately all I see are 404 errors because it couldn't find some JS/CSS file. Is there a config I'm missing?

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

Updated: Here is the index.html file (auto-generated via 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>

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

Here are the errors I'm seeing in Chrome Web Inspector when visiting 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文件的原因?

One more thing I forgot to mention. When I generate the war using mvn clean package, all the files that are under src/main/resources/public are placed into the WEB-INF/classes/resource subfolder. According to research, these files are not publicly visible (i.e. if I try to access localhost:8080/my-app/foo.css, it'll give 404). Is this why the index.html file is unable to "see" the JS/CSS files it depends on?

推荐答案

我最终弄清楚了这个问题,但没有使用Spring Boot打包WAR文件.我的本地上还有另一个项目,该项目使用普通的Maven + pom.xml + web.xml来创建WAR,我以此为参考来弄清楚为什么当前项目无法正常工作.有多个问题:

I ended up figuring it out but not using Spring Boot to package the WAR file. There was another project lying around on my local that used plain old Maven + pom.xml + web.xml to create WARs, which I used as a reference to figure out why the current project was not working. There were multiple issues:

  • 当使用默认配置部署到Tomcat 8时,它将在其路径后附加WAR文件的名称(它们称为Context).在这种情况下,它是http://localhost:8080/my-app. 已编译"的AngularJS应用程序的index.html具有<base href="/">标记,该标记需要指向/my-app/而不是/.这是在Web Inspector > Sources中看不到JS/CSS文件的主要原因.
  • <link>标记的src属性不应包含前导/
  • 对于上面发布的Spring Boot应用程序,它带有嵌入式Tomcat,并在Root Context上部署了该应用程序,因此无需更改index.html文件中的任何路径.
  • 类似于Spring Boot,因为没有创建子上下文",所以我在ExpressJS中运行应用程序也没有问题.同样,在这种情况下,无需修改任何文件.
  • When you deploy onto Tomcat 8 using default config, it will append the name of the WAR file (what they refer to as Context) to its path. In this case, it was http://localhost:8080/my-app. The "compiled" AngularJS app's index.html had a <base href="/"> tag that needed to point to /my-app/ instead of /. This was the main reason why the JS/CSS files were not visible in Web Inspector > Sources.
  • <link> tag's src attribute was not supposed to contain a leading /
  • In the case of the Spring Boot App I posted above, it comes with an Embedded Tomcat and deploys the app at the Root Context so there's no need to change any paths in the index.html file.
  • Similar to Spring Boot, I also had no issues running the app in ExpressJS since a "sub-context" was not created. Again, there was no need to modify any files in this case.

还有其他与查找诸如.ttf文件之类的资源有关的错误,但至少该应用能够运行.

There were other errors related to finding resources like .ttf files but at least the app was able to run.

更新: 通过在server.xml文件底部附近添加以下内容,似乎可以从Tomcat 8根目录提供WAR文件:

Update: Looks like its possible to serve the WAR file from the Tomcat 8 root by adding the following near the bottom of the server.xml file:

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

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

This will prevent the need to modify the index.html file.

太好了:)

这篇关于访问资源时将Spring Boot WAR部署到Tomcat 8-HTTP 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆