Spring-React frontend-maven-plugin 不工作

2023-12-05

我正在关注的文档是https://spring.io/guides/tutorials/react-and-spring-data-rest/构建一个使用 Spring 的 React 应用程序。 spring 部分很好,直到到达建议使用插件安装 node 和 npm 模块的部分。我的问题是这个插件没有做它应该做的事情。我检查了文档并确定了一些执行(我真的不知道插件是如何工作的)。我介绍了这些执行,但我仍然看不到 React 应用程序在浏览器中的 localhost:8080 处渲染。

这是他们在 spring 文档中使用的插件。就这样。我希望任何遵循本教程的人都可以帮助我。

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
</plugin>

您可以在同一端口上运行 React Frontend 和 SpringBoot Backend 并将它们打包为单个工件!

这里是Github我要访问的演示项目的链接 在这里解释一下

Spring Boot 可以提供静态内容src/main/resources/static文件夹。我们将利用 Spring Boot 的上述功能来服务 React 项目的单页面。我们将从目标目录中的静态文件夹(而不是源目录中)提供 html 页面。

The 项目结构-

enter image description here

首先,创建一个 Spring Boot 项目https://start.spring.io。添加 Web 依赖项。将 groupId 和 artifactId 设置为您想要的任何值。生成项目并将其解压缩到您的项目目录中。

或者,如果您使用的是 Spring Tools Suite,您只需单击File->New->Spring Starter Project并提及创建 Spring Boot 项目所需的详细信息。

The frontend里面的文件夹src/main应该让你的反应应用程序构建使用创建反应应用程序.

所以,有两个步骤——

  1. 创建前端的生产版本。
  2. 将生产版本复制到 ${target/classes/} 中。

我们将使用两个 Maven 插件Thymleaf为了那个原因。

  1. 前端 maven 插件对于步骤 1。
  2. maven-资源-插件对于步骤 2。

For 前端 maven 插件在第 1 步——如果你仔细观察pom.xml我在那里提到了src目录来自哪里前端 maven 插件将获取文件,创建生产版本并将内容放入提到的输出目录中(内部src/main/frontend/build).

 <workingDirectory>${frontend-src-dir}</workingDirectory>
 <installDirectory>${project.build.directory}</installDirectory>

For maven-资源-插件在第 2 步——它将采用刚刚创建的生产版本前端 maven 插件然后将其放入根目录中target/classes/static.

然后我们将使用Thymleaf提供静态内容target/classes/static在控制器中使用休息端点。否则您必须输入姓名html file, like http://localhost:8080/index.html

Your 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.springreact</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Run React Frontend and SpringBoot Backend on the same port.</description>
    <properties>
        <java.version>1.8</java.version>
        <frontend-src-dir>${project.basedir}/src/main/frontend</frontend-src-dir>
        <node.version>v14.15.4</node.version>
        <yarn.version>v1.16.0</yarn.version>
        <frontend-maven-plugin.version>1.7.6</frontend-maven-plugin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>${frontend-maven-plugin.version}</version>

                <configuration>
                    <nodeVersion>${node.version}</nodeVersion>
                    <yarnVersion>${yarn.version}</yarnVersion>
                    <workingDirectory>${frontend-src-dir}</workingDirectory>
                    <installDirectory>${project.build.directory}</installDirectory>
                </configuration>

                <executions>
                    <execution>
                        <id>install-frontend-tools</id>
                        <goals>
                            <goal>install-node-and-yarn</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>yarn-install</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>

                    <execution>
                        <id>build-frontend</id>
                        <goals>
                            <goal>yarn</goal>
                        </goals>
                        <phase>prepare-package</phase>
                        <configuration>
                            <arguments>build</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>position-react-build</id>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <phase>prepare-package</phase>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${frontend-src-dir}/build</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这里是控制器 code.

package com.springreact.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

    @GetMapping("")
    public ModelAndView home() {
        ModelAndView mav=new ModelAndView("index");
        return mav;
    }

}

如果您按照上述步骤操作,您应该会看到您的 React 应用程序正在启动http://localhost:8080/.

enter image description here

如果您仍然有疑问,那么您可以查看我写的综合博客。以下是两个不同平台上博客的链接,您可以选择您喜欢的。

开发社区-https://dev.to/arpan_banerjee7/run-react-frontend-and-springboot-backend-on-the-same-port-and-package-them-as-a-single-artifact-14pa

Medium- https://arpan-banerjee7.medium.com/run-react-frontend-and-springboot-backend-on-the-same-port-and-package-them-as-a-single-artifact-a790c9e10ac1

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring-React frontend-maven-plugin 不工作 的相关文章

随机推荐

  • MVC3 EF 工作单元 + 通用存储库 + Ninject

    我是 MVC3 的新手 一直在关注 asp net 网站上的精彩教程 然而 我不太清楚如何将工作单元和通用存储库模式与 Ninject 结合使用 我使用本教程作为起点 http www asp net mvc tutorials getti
  • 使用设备策略控制器在后台升级应用程序

    我有一个正在运行的 DPC 应用程序 它是设备所有者 我已在两台不同的 Android 6 0 1 设备上尝试过此操作 以排除任何设备 制造商问题 I used adb shell dpm set device owner com exam
  • VB.NET 中默认启用选项 Strict

    每当我创建一个新的 VB NET 程序时 我必须进入该项目的属性并将 Option strict 设置为打开 我可以这样做一次 这样每次创建新项目时它都是默认的吗 在 Visual Studio 中 转到菜单Tools gt Options
  • 训练后,TensorFlow 始终会收敛到所有项目的相同输出

    这是我正在使用的代码片段 import tensorflow as tf import numpy as np from PIL import Image from os import listdir nodes l1 500 nodes
  • Log4j2 覆盖过去一天的日志文件

    我正在使用 Log4j2 版本 2 3 log4j2 xml 如下所示
  • 使第二行填补上面的空白

    一个简单的 html css 问题 请看这个example 我希望第二行中的块能够填补它们上方的空白 除了使用 JavaScript 之外还有什么办法吗 block float left width 200px height 200px b
  • Lasagne 与 Theano 可能版本不匹配(Windows)

    所以我终于设法让 theano 启动并在 GPU 上运行this指导 测试代码运行良好 告诉我它使用了 GPU 耶 然后我想尝试一下并遵循this数字识别 CNN 训练指南 问题是 我从烤宽面条调用 theano 的方式中收到错误 我猜这里
  • 有没有办法进行内联多值比较?

    我什至觉得问这个问题很愚蠢 因为这看起来很微不足道 但我的大脑却让我失望了 如果我有以下内容 let a b c 1 1 1 有没有一种优雅的方法来确定 a b 和 c 是否都具有相同的值 就像是 let result a b c 这会失败
  • python中如何检查两个字符串是否有交集?

    例如 a abcdefg b krtol 它们没有交集 c hflsfjg 则a和c有交集 检查这个最简单的方法是什么 只需要一个 True 或 False 结果 def hasIntersection a b return not set
  • 使用powershell登录后如何从网站获取表数据?

    我的公司希望我从他们的内部网站获取数据 对其进行组织 然后将其发送到数据库 数据显示在您在站点内导航到的表格上 我想将这些字段提取到文件或内存中以进行进一步处理 到目前为止 我可以通过获取提交登录按钮的 ID 并传递我的用户名 密码来在 p
  • ffmpeg通过python子进程无法找到相机

    这里有一个奇怪的问题 我使用这个命令通过 ffmpeg 捕获我的网络摄像头 通过 Windows 上的 cmd ffmpeg y t 300 rtbufsize 1024M f dshow i video Lenovo EasyCamera
  • 视图需要相互依赖的逻辑:没有模型是否可行?

    我正在尝试编写一些 Oracle 11g SQL 但遇到了一些先有鸡还是先有蛋的问题 我正在寻找类似电子表格的行为 我找到了一个使用 Oracle 的解决方案MODEL条款 但性能并不好 所以我想知道是否 非MODEL 解决方案在技术上甚至
  • 在 django 项目中导入应用程序

    我在 django 项目中的另一个应用程序中导入应用程序时遇到问题 我知道有几个关于这个主题的问题 asnwsers 相信我 我读了很多 甚至还有一些关于 python import 的 这是我的项目树 我将放置真实的文件夹名称 was f
  • 将 URL 变量传递到 xsl 中

    是否可以将 URL 变量传递到 xsl xml 中 例如 http www somedomain com index aspx myVar test myVar2 anotherTest 我希望能够在 xsl 文件的逻辑中使用 myVar
  • 在 Nifi 中从 Avro Schema 创建 Postgresql 表

    使用 InferAvroSchema 我获得了文件的 Avro 架构 我想使用此 Avro 架构在 PostregSql 中创建一个表 我必须使用哪个处理器 我使用 GetFile gt InferAvroSchema gt 我想从此架构创
  • ItemsControl 调整大小时边框消失(使用“stretchpanel”)

    昨天我问如何让 ItemsControl 使其子项均匀分布在可用空间中 在愉快地阅读答案后 我写了 实际上是复制并调整了它 我自己的 stretchpanel 它正是我想要的 然而 我的孩子应该在右侧有一个边框 只要孩子比其内容大 它就可以
  • 与 python pandas 中的melt相反

    我不知道如何在 python 中使用 Pandas 进行 反向熔化 这是我的起始数据 label type value 0 x a 1 1 x b 2 2 x c 3 3 y a 4 4 y b 5 5 y c 6 6 z a 7 7 z
  • 如何解决 AMD 路径冲突?

    我正在尝试使用Esri ArcGis JavaScript API 其加载方式为Dojo using dojo require 我有一个现有的模块化AMD requirejs我需要将此代码集成到其中的 Typescript 应用程序 在初始
  • 如何获取点击“赞”按钮的用户的 Facebook ID?

    有没有办法获取在我自己的网站上点击社交插件的 赞 按钮的用户的 Facebook ID 尽管我花了一整天的时间寻找它 但我找不到任何方法 据我所知 您不可能知道谁点击了 喜欢 按钮 但是如果用户通过您的网站登录 Facebook 意味着您编
  • Spring-React frontend-maven-plugin 不工作

    我正在关注的文档是https spring io guides tutorials react and spring data rest 构建一个使用 Spring 的 React 应用程序 spring 部分很好 直到到达建议使用插件安装