Gus*_*nez 1 spring spring-mvc reactjs spring-boot
我按照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>
Run Code Online (Sandbox Code Playgroud)
您可以在同一端口上运行 React Frontend 和 SpringBoot Backend 并将它们打包为单个工件!
这是我将在这里解释的演示项目的Github链接
Spring Boot 可以提供src/main/resources/static文件夹中的静态内容。我们将利用 Spring Boot 的上述功能来服务 React 项目的单页面。我们将从目标目录中的静态文件夹(而不是源目录中)提供 html 页面。
项目结构-
首先,使用https://start.spring.io创建一个 spring boot 项目。添加 Web 依赖项。将 groupId 和 artifactId 设置为您想要的任何值。生成项目并将其解压缩到您的项目目录中。
或者,如果您使用 Spring Tools Suite,您只需单击
File->New->Spring Starter Project并提及所需的详细信息即可创建 Spring Boot 项目。
frontend里面的文件夹应该src/main包含使用 create-react-app构建的 React 应用程序。
所以,有两个步骤——
我们将使用两个 Maven 插件和Thymleaf。
对于第 1 步中的frontend-maven-plugin -如果您仔细查看pom.xml那里,我提到了frontend-maven-pluginsrc将从中获取文件的目录,创建生产版本并将内容放入提到的输出目录中(内部)。src/main/frontend/build
<workingDirectory>${frontend-src-dir}</workingDirectory>
<installDirectory>${project.build.directory}</installDirectory>
Run Code Online (Sandbox Code Playgroud)
对于第 2 步中的maven-resources-plugin -它将采用刚刚由frontend-maven-plugin创建的生产构建并将其放置在您的根目录中target/classes/static。
然后,我们将使用Thymleaftarget/classes/static通过控制器中的休息端点来提供静态内容。否则你必须输入名称html file,例如http://localhost:8080/index.html
你的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>
Run Code Online (Sandbox Code Playgroud)
这是控制器代码。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您按照上述步骤操作,您应该会看到您的 React 应用程序正在启动http://localhost:8080/。
如果您仍然有疑问,那么您可以查看我写的综合博客。以下是两个不同平台上博客的链接,您可以选择您喜欢的。
| 归档时间: |
|
| 查看次数: |
8679 次 |
| 最近记录: |