NVIDIA 初创加速计划,免费加速您的创业启动 了解详情
写点什么

用 Spring Boot 打包你的 React 应用

  • 2020-03-31
  • 本文字数:3612 字

    阅读完需:约 12 分钟

用Spring Boot打包你的React应用

先讲一讲这篇文章的背景故事。之前我的团队需要在我们需求的基础架构上节省一些资金,并且由于我们要构建的这个应用程序中,大部分负载都会在客户端而非服务端上,所以我们决定试验一下能否将一个 Spring 应用程序与一个 React 应用结合起来,并打包成一个 war 文件。


这篇文章会告诉你如何将 Create React App 与 Spring Boot 结合使用,从而为你打包出单个 war 文件。


Spring Boot 和 Create React App 的基本功能介绍


  • Create React App 可帮助你非常快速地启动 React 项目。它为你提供了启动并尽快运行项目所需的全部基本功能。

  • Spring Boot 可以帮助你快速而轻松地启动和维护 Spring 应用程序。

步骤

1.目标

  • 在单一 war 文件中包含前端和后端,具有优化的生产构建

  • 保留 Create React App 所提供的好处,如热重载等

2.设置


附注:我选择的 IDE 是 IntelliJ。当使用 React 代码时,我通常会切换到 VS Code。你可以随意使用自己习惯的方法


  • 在 Github 上创建一个空的仓库,并添加自述文件、gitignore 和许可证等。

  • 转到(https://start.spring.io)创建你的 Spring 应用程序,并下载到本地。Group 和 Artifact 也可以随意设置。



GroupId:e.the.awesome


Artifact:spring-react-combo-app

3.将下载的项目解压缩到你的 git 目录中并提交。

你的 SpringReactComboAppApplication 看起来应该像这样。


package e.the.awesome.springreactcomboapp;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplicationpublic class SpringReactComboAppApplication extends SpringBootServletInitializer{
public static void main(String[] args) { SpringApplication.run(SpringReactComboAppApplication.class, args); }
}
复制代码

4.现在创建一个基本服务。

我们将其称为 DadJokesController。这应该在与 SpringReactComboAppApplication 文件所在的文件夹中创建。我知道这不是正确的 Rest API 格式,但现在暂时忽略它。


package e.the.awesome.springreactcomboapp;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DadJokesController {    @GetMapping("/api/dadjokes")    public String dadJokes() {        return "Justice is a dish best served cold, if it were served warm it would be just water.";    }}
复制代码

5.在你的终端运行

mvn spring-boot:run
复制代码


然后在浏览器中检查http://localhost:8080/api/dadjokes。你应该会看到我们添加到控制器中的 dad joke。

6.要创建你的 React 应用,只需在根目录中运行

npx create-react-app basic-frontend-app
复制代码


你可以随心所欲地调用它,我这里只调用我的 basic-frontend-app

7.要运行前端应用程序:

cd basic-frontend-app<br>npm start
复制代码

8.解决代理问题

由于我们要将 Dad Jokes 服务集成到前端,因此首先我们要解决代理问题。你可能已经注意到了,你的服务从 localhost:8080 开始,而前端从 localhost:3000 开始。如果我们尝试从前端调用服务,具体取决于你的浏览器,你可能会收到一个 CORS 错误。



解决此问题的最简单方法是让前端代理处理从端口 3000 到 8080 的所有请求。此更改将在 package.json 文件中进行:


{  "name": "basic-frontend-app",  "version": "0.1.0",  "private": true,  "dependencies": {    "react": "^16.3.1",    "react-dom": "^16.3.1",    "react-scripts": "1.1.4"  },  "scripts": {    "start": "react-scripts start",    "build": "react-scripts build",    "test": "react-scripts test --env=jsdom",    "eject": "react-scripts eject"  },  "proxy": {    "/api": {      "target": "http://localhost:8080",      "ws": true    }  }}
复制代码


将以下内容添加到你的前端 App.js 文件中


import React, {Component} from 'react';import logo from './logo.svg';import './App.css';
class App extends Component {
state = {};
componentDidMount() { this.dadJokes() }
dadJokes = () => { fetch('/api/dadjokes') .then(response => response.text()) .then(message => { this.setState({message: message}); }); };
render() { return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo"/> <h3 className="App-title">{this.state.message}</h3> </header> <p className="App-intro"> To get started, edit <code>src/App.js</code> and save to reload. </p> </div> ); }}
export default App;
复制代码


如果你也遇到了下图的这个错误,我的做法是:首先删除了 package-lock.json 文件;其次在 node_modules 文件夹中重新安装了 npm 包并再次运行;然后重新启动前端,问题就解决了。


9.你的应用程序现在应该看起来像这样。

你可以看到 dad jokes API 调用的结果。


10.创建生产版本

现在我们的基本前端和后端已经完成,该创建生产版本和单个 war 文件了。


< dependencies>下添加


<!-- https://mvnrepository.com/artifact/com.github.eirslett/frontend-maven-plugin --><dependency><groupId>com.github.eirslett</groupId><artifactId>frontend-maven-plugin</artifactId><version>1.6</version></dependency>
复制代码


在 pom 文件的< plugins>部分下,我们将添加以下命令,这些命令将在运行 mvn clean install 时执行以下操作。


  • npm 安装指定版本的 node

  • 运行我们前端的生产构建

  • 存放生产构建


<plugin>   <groupId>com.github.eirslett</groupId>   <artifactId>frontend-maven-plugin</artifactId>   <version>1.6</version>   <configuration>      <workingDirectory>basic-frontend-app</workingDirectory>      <installDirectory>target</installDirectory>   </configuration>   <executions>      <execution>         <id>install node and npm</id>         <goals>            <goal>install-node-and-npm</goal>         </goals>         <configuration>            <nodeVersion>v8.9.4</nodeVersion>            <npmVersion>5.6.0</npmVersion>         </configuration>      </execution>      <execution>         <id>npm install</id>         <goals>            <goal>npm</goal>         </goals>         <configuration>            <arguments>install</arguments>         </configuration>      </execution>      <execution>         <id>npm run build</id>         <goals>            <goal>npm</goal>         </goals>         <configuration>            <arguments>run build</arguments>         </configuration>      </execution>   </executions></plugin><plugin>   <artifactId>maven-antrun-plugin</artifactId>   <executions>      <execution>         <phase>generate-resources</phase>         <configuration>            <target>               <copy todir="${project.build.directory}/classes/public">                  <fileset dir="${project.basedir}/basic-frontend-app/build"/>               </copy>            </target>         </configuration>         <goals>            <goal>run</goal>         </goals>      </execution>   </executions></plugin>
复制代码


附注:对于你的插件来说,顺序正确是很重要的,因此请确保在复制构建文件执行之前执行 node/npm 安装

11.运行 mvn clean install

添加此命令后,运行 mvn clean install,并验证 target/classes 目录同时包含了前端文件和 Java 文件。这里你应该是一路畅通的。


最后看一下我的 pom 文件:


https://github.com/Emmanuella-Aninye/Spring-Boot-ReactJS-Starter/blob/master/pom.xml


这就是我的方法。如果你想看看 repo 或使用它,可以在我的 Github 上找到它:


https://github.com/Emmanuella-Aninye/Spring-Boot-ReactJS-Starter


延伸阅读:


https://hackernoon.com/package-your-react-app-with-spring-boot-a-how-to-guide-cdfm329w


2020-03-31 09:522912

评论

发布
暂无评论
发现更多内容

LabVIEW纹理分析(基础篇—9)

不脱发的程序猿

机器视觉 图像处理 LabVIEW 纹理分析

Go 通过 Map/Filter/ForEach 等流式 API 高效处理数据

万俊峰Kevin

微服务 stream go-zero Go 语言

03 Prometheus之架构及数据模型

穿过生命散发芬芳

Prometheus 1月月更

千万级学生管理系统的考试试卷存储方案

swallowluo

架构实战营 #架构实战营 「架构实战营」

首次!中西方数据库大咖“时空对话”,为中国分布式数据库开发者大会打call

OceanBase 数据库

OceanBase 开源 OceanBase 社区版 开发者大会

设计微博评论的高性能高可用计算架构

drizzle

「架构实战营」

【LeetCode】统计特殊四元组Java题解

Albert

算法 LeetCode 1月月更

在线JSON转CSV,TSV工具

入门小站

工具

盘点 2021 | 云原生拥抱之路

xcbeyond

程序人生 云原生 盘点2021 xcbeyond

(1-1/1)底层逻辑读后感:三种对错观四类表述

mtfelix

无限生长 2022Y300P

川大记忆

wood

300天创作 川大

Prometheus云原生监控:运维与开发实战

方勇(gopher)

云原生 #Prometheus

架构实战训练营-模块5-作业

温安适

「架构实战营」

《张汉东的 Rust 实战课》学习笔记

贾献华

hw8-设计消息队列存储消息数据的MySQL表格

WWH

架构实战营

java 组件开发的准备工作

努力努力再努力

1月月更

一份简单够用的 Nginx Location 配置讲解

冴羽

nginx 后端 博客 后端开发 博客配置

盘点 2021|一个普通人的不普通的2021

慕枫技术笔记

程序人生 盘点 2021

世界女性科技群落(五):数字化黄金时代,东南亚女性都是隐藏的阿尔法

脑极体

分布式系统必知必会

小梁编程汇

分布式 分布式系统 共识算法 分布式通信算法 #raft

C++ 动态内存分配的问题,你都懂了吗?

小梁编程汇

c++ 堆内存管理 内存分配 smart pointer

滴滴夜莺监控发布v5正式版,定位Prometheus企业版

龙渊秦五

Prometheus zabbix Open-Falcon Nightingale

一个cpp协程库的前世今生(九)协程参数与返回值的处理

SkyFire

c++ cocpp

【新年互动搞起!】元旦快乐!这里是2022年的 OceanBase

OceanBase 数据库

开发者 OceanBase 社区版 2022年新年祝福 元旦

写时复制技术(COW)详解

小梁编程汇

性能优化 操作系统 CopyOnWrite;

SpringMVC框架基础知识(02)

海拥(haiyong.site)

28天写作 1月月更

Flutter 让你的Dialog脱胎换骨吧!(Attach,Dialog,Loading,Toast)

小呆呆666

flutter ios android 前端 大前端

盘点 2021|考研,裸辞——混乱中寻找秩序,2021后记

某个Coder

盘点 2021

获奖公布!OceanBase 第一届技术征文大赛圆满收官!

OceanBase 数据库

开发者 征文大赛 oceanbase OceanBase 开源

(1-2/2)AI的落地:读caoz的文章有感

mtfelix

无限生长 2022Y300P

架构训练营 week4 作业

红莲疾风

「架构实战营」

用Spring Boot打包你的React应用_大前端_Emmanuella_InfoQ精选文章