[英]Deploying Spring Boot WAR to Tomcat 8 - HTTP 404 when accessing resources
我是Spring Boot的新手,並且正在努力將一個簡單的HTML Web應用程序(AngularJS)部署到Tomcat 8.這個Web應用程序只是提供一些HTML / CSS / JS內容而沒有對后端的REST調用。 它已經使用Webpack進行“編譯” - 這會生成JS / CSS包和一個通過<script> / <link>
標記指向它們的index.html文件 - 並且已經在ExpressJS和Spring Boot w / Embedded上進行了測試tomcat並按預期工作。 但是沿着獨立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部署的步驟:
mvn clean package
生成WAR 不幸的是我看到的都是404錯誤,因為它找不到一些JS / CSS文件。 有沒有我失蹤的配置?
更新 :這是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>
以下是我在訪問localhost:8080/my-app/index.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
生成war時,src / main / resources / public下的所有文件都放在WEB-INF/classes/resource
子文件夾中。 根據研究,這些文件不是公開可見的(即如果我嘗試訪問localhost:8080 / my-app / foo.css,它將給出404)。 這就是index.html文件無法“看到”它依賴的JS / CSS文件的原因嗎?
在沒有更多上下文的情況下,您所描述的設置的最常見問題是您最有可能使用絕對URL指向您的js / css文件。 因此,當您將它們放入/my-app
下的servlet容器中時,它們不再正確引用,因為它們正在嘗試轉到根/
。 在描述路徑上資源文件的位置時,需要使用相對URL。
我最終搞清楚但沒有使用Spring Boot打包WAR文件。 我的本地還有另一個項目,它使用普通的舊Maven + pom.xml + web.xml
來創建WAR,我將其用作參考,以找出當前項目無法正常工作的原因。 存在多個問題:
http://localhost:8080/my-app
。 “已編譯”的AngularJS應用程序的index.html
有一個<base href="/">
標記,需要指向/my-app/
而不是/
。 這是在Web Inspector > Sources
不到JS / CSS文件的主要原因。 <link>
標簽的src
屬性不應包含前導/
還有其他與查找.ttf
文件等資源相關的錯誤,但至少應用程序能夠運行。
更新:看起來可以通過在server.xml
文件底部附近添加以下內容來從Tomcat 8根目錄提供WAR文件:
<Context path="" docBase="my-app" debug="0" reloadable="true"></Context>
這樣可以防止需要修改index.html文件。
太棒了:)
我有同樣的問題,它與Webpack相關而不是Spring-boot。 在您的webpack.common.js / webpack.prod.js中,您必須設置:
metada.baseUrl = './';
並將其注入index.html中的<base>
鏈接
你也必須設置
output.publicPath: './'
有了這兩個變量集,這對我有用。
嗯,我在spring-boot
項目中試過了一些。
首先,我將對我的項目pom.xml
添加一個依賴項
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
然后我在src/main/resources/templates
上添加一個index.html
之后,您應該添加一個控制器以指向該索引
@Controller
public class AppController {
@RequestMapping("/")
String index() {
return "index";
}
}
有關更多信息,請參閱本指南。 彈簧靴和Thymeleaf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.