【问题标题】:Spring boot backend with angular frontend带有角度前端的 Spring Boot 后端
【发布时间】:2021-05-15 22:11:29
【问题描述】:

我已经创建了一个 spring boot 后端和一个 angular 前端,现在我想使用 sprint boot 在 8080 上运行,其主页显示 index.html(来自 angular)。

我使用 ng build --prod 命令在 Angular 中创建了 dist 文件夹并复制到 (src/main/resources/static) 文件夹中。enter image description here

除此之外,我还阅读了许多关于此的 stackoverflow 帖子,并尝试通过添加来创建一个 war 文件

<packaging>war</packaging>

在 pom.xml 中(虽然我不知道如何创建战争) 事实上,它在目标文件夹中创建了一个战争文件(AIeDD-0.0.1-SNAPSHOT)。 有些帖子说要在tomcat服务器上部署战争,但我不知道该怎么做。

现在我不知道该怎么办??

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.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cdac</groupId>
    <artifactId>AIeDD</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>AIeDD</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

    标签: java angular spring spring-boot


    【解决方案1】:

    与所有 Spring-Applications 一样,您基本上有两个选择。您可以将其打包为 war(没有捆绑的应用程序服务器)并将其部署在一个上。或者您可以将其打包为 jar(所谓的 fat-jar)并与捆绑的应用程序服务器一起交付。默认是tomcat。

    然后你可以保持你的构建过程:创建一个 dist 文件夹并将内容复制到你的资源文件夹中。然后,您拥有的控制器只是将文件的“路径”作为字符串返回。另一种方法是使用名为“buildfrontend-maven-plugin”的具体 maven 插件。有了这个,您可以构建您的 jar,而 maven 负责构建 Angular 应用程序。

    Spring-Boot with Angular

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2019-11-16
      • 1970-01-01
      • 2018-09-28
      • 2014-04-12
      • 2016-11-05
      • 2021-04-04
      • 2017-10-25
      • 2020-11-05
      相关资源
      最近更新 更多