Ich bin neu bei Spring Boot und habe Schwierigkeiten, eine einfache HTML-Webanwendung (AngularJS) für Tomcat 8 bereitzustellen. Diese Webanwendung dient lediglich als HTML/CSS/JS Inhalt ohne REST-Aufrufe an ein Back-End. Es wurde mit Webpack "kompiliert" - das erzeugt JS/CSS-Bundles und eine einzige index.html-Datei, die über <script>/<link>
Tags auf sie verweist - und wurde auf ExpressJS und Spring Boot mit Embedded Tomcat getestet und funktioniert wie erwartet. Aber den Weg eines eigenständigen WAR zu gehen und auf Tomcat 8 zu verteilen, scheint nicht richtig zu funktionieren.Bereitstellen von Spring Boot WAR auf Tomcat 8 - HTTP 404 beim Zugriff auf Ressourcen
<?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>
:
für Frühjahr Boot-Projekt habe ich alle HTML/CSS/JS-Dateien im Ordner src/main/resources/public
(keine Unterordner) und haben auch die pom.xml
wie folgt (Grund config) konfiguriert enthalten "Main" Klasse:
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);
}
}
Hier sind die Schritte, die ich verwendet, um die Tomcat-Bereitstellung zu tun:
mvn clean package
, um den Krieg- Kopieren Sie die WAR-Pfad zu erzeugen/zu/tomcat8/Webapp
- starten Tomcat
- http://localhost:8080/my-app => Auto-Lasten index.html
Leider 404 Fehler sind alles, was ich sehen weil es keine JS/CSS-Datei finden konnte. Gibt es eine Konfiguration, die ich vermisse?
Aktualisiert: Hier ist die Datei index.html (automatisch generiert über 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>
Hier sind die Fehler, die ich in Chrome Web Inspector bin zu sehen beim Besuch 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)
Eine Sache, die ich vergessen zu erwähnen. Wenn ich den Krieg mit mvn clean package
erzeuge, werden alle Dateien, die sich unter src/main/resources/public befinden, in den Unterordner WEB-INF/classes/resource
gestellt. Laut den Recherchen sind diese Dateien nicht öffentlich sichtbar (d. H., Wenn ich versuche, auf localhost zuzugreifen: 8080/my-app/foo.css, wird 404 angezeigt). Kann die Datei index.html die JS/CSS-Dateien, von denen sie abhängt, nicht "sehen"?
Geben Sie mehr Kontext darüber, was der vollständige 404-Fehler ist, oder schließen Sie Ihre index.html-Seite ein, damit die Benutzer nachvollziehen können, wie die Ressourcendateien geladen werden. –