无法将 Angular 应用程序部署到 Spring 启动
Unable to deploy Angular app to Spring Boot
我有一个 Angular 应用程序以及一个 API 使用 Spring 启动的应用程序,我需要将其打包为单个 war 文件以部署到 apache 上tomcat。我试过:
构建我的 angular 应用程序并将构建的文件粘贴到我的 Spring 应用程序中
使用 maven war 插件将文件构建成 war
我看到的一些教程似乎显示人们将构建的 angular 文件复制到 Spring 静态文件夹,然后 运行 spring 并且它有效。这对我来说似乎很奇怪,因为没有配置映射并且也不起作用。对我来说。
我认为我最好的选择是使用 maven war 插件,但我的问题是:
我构建的 wars 在部署到 tomcat
时无法通过浏览器访问
它们导致 The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
错误
我只是不知道下一步该做什么
我希望有人能在这里为我指明正确的方向。提前致谢。
我已根据答案进行了一些更改,我的 war 文件正在编译并被 tomcat 找到,但是,angular 应用无法找到任何其他资源并且只加载了一个空白页。在开发工具中查找样式和其他文件时,我得到 404。
我现在正在使用不同名称的目录副本。在我的 package.json 中,我这样做:
{
"name": "task-app-front-end",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxyconfig.json",
"build": "ng build",
"test": "ng test",
"outDir": "../taskappmain\src\main\resources\public",
"lint": "ng lint",
"e2e": "ng e2e"
}
正在将我构建的文件输出到我的后端应用程序的 /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
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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>taskappmain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>taskappmain</name>
<description>taskAppMain</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</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>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/public</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/public</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您的构建顺序应如下所示才能正常工作:
- 构建前端应用程序(Angular)
- 构建 REST 应用程序(Spring 引导应用程序)
您需要执行此操作,因为您将需要 angular 应用程序中的 dist
文件夹,其中包含前端的生产代码。
在您的 maven 构建中组装 war 文件时,您需要执行以下操作:
- 首先确保maven
下prepare-compile和compile阶段有运行
- 在
Prepare package
阶段,您需要将 front-end/dist/web
中的文件复制到 target/classes/static/
下的项目 backend
中
可以使用 Maven Resources Plugin 或 Maven AntRun Plugin 自动完成复制操作。
复制到 target
目录而不是 src/main/resources/static
的原因是 Angular 应用程序的分发不是 'source' 后端代码。
至于为什么static
包中的任何文件都可以直接在你的WAR中使用,这是因为Spring默认情况下引导有一个静态资源绑定,允许所有文件在static
包无法通过互联网访问。另请参阅 Spring 引导本身的 Sample project。
对于在 Tomcat 中部署后后端无法正常工作的问题,这可能是由于 Tomcat 不知道如何启动您的应用程序。请记住 Spring Boot 默认情况下放弃使用 web.xml
。因此,要允许容器服务器 (Tomcat) 启动 Spring 引导,您需要将以下内容添加到带有 @SpringBootApplication
注释的 class
public class MyRestApp extends SpringBootSerlvetInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MyRestApp.class);
}
}
我有一个 Angular 应用程序以及一个 API 使用 Spring 启动的应用程序,我需要将其打包为单个 war 文件以部署到 apache 上tomcat。我试过:
构建我的 angular 应用程序并将构建的文件粘贴到我的 Spring 应用程序中
使用 maven war 插件将文件构建成 war
我看到的一些教程似乎显示人们将构建的 angular 文件复制到 Spring 静态文件夹,然后 运行 spring 并且它有效。这对我来说似乎很奇怪,因为没有配置映射并且也不起作用。对我来说。
我认为我最好的选择是使用 maven war 插件,但我的问题是:
我构建的 wars 在部署到 tomcat
时无法通过浏览器访问
它们导致
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
错误我只是不知道下一步该做什么
我希望有人能在这里为我指明正确的方向。提前致谢。
我已根据答案进行了一些更改,我的 war 文件正在编译并被 tomcat 找到,但是,angular 应用无法找到任何其他资源并且只加载了一个空白页。在开发工具中查找样式和其他文件时,我得到 404。
我现在正在使用不同名称的目录副本。在我的 package.json 中,我这样做:
{
"name": "task-app-front-end",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxyconfig.json",
"build": "ng build",
"test": "ng test",
"outDir": "../taskappmain\src\main\resources\public",
"lint": "ng lint",
"e2e": "ng e2e"
}
正在将我构建的文件输出到我的后端应用程序的 /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
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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>taskappmain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>taskappmain</name>
<description>taskAppMain</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</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>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/public</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/public</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您的构建顺序应如下所示才能正常工作:
- 构建前端应用程序(Angular)
- 构建 REST 应用程序(Spring 引导应用程序)
您需要执行此操作,因为您将需要 angular 应用程序中的 dist
文件夹,其中包含前端的生产代码。
在您的 maven 构建中组装 war 文件时,您需要执行以下操作:
- 首先确保maven 下prepare-compile和compile阶段有运行
- 在
Prepare package
阶段,您需要将front-end/dist/web
中的文件复制到target/classes/static/
下的项目
backend
中
可以使用 Maven Resources Plugin 或 Maven AntRun Plugin 自动完成复制操作。
复制到 target
目录而不是 src/main/resources/static
的原因是 Angular 应用程序的分发不是 'source' 后端代码。
至于为什么static
包中的任何文件都可以直接在你的WAR中使用,这是因为Spring默认情况下引导有一个静态资源绑定,允许所有文件在static
包无法通过互联网访问。另请参阅 Spring 引导本身的 Sample project。
对于在 Tomcat 中部署后后端无法正常工作的问题,这可能是由于 Tomcat 不知道如何启动您的应用程序。请记住 Spring Boot 默认情况下放弃使用 web.xml
。因此,要允许容器服务器 (Tomcat) 启动 Spring 引导,您需要将以下内容添加到带有 @SpringBootApplication
public class MyRestApp extends SpringBootSerlvetInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MyRestApp.class);
}
}