繁体   English   中英

如何从Spring Boot应用程序为React应用程序提供服务?

[英]How do I serve a React application from an Spring Boot application?

我想从Spring Boot服务器应用程序提供ReactJS UI应用程序的Webpack构建的结果。

当Webpack构建生成“ dist”文件夹中的所有资产时,即index.html,bundle.js等...以http://localhost/index.html的身份访问

Spring Boot应用程序从http:// localhost / api / xxx提供API URL

  1. 在react项目的根目录中创建.env文件(在package.json之后)
  2. PUBLIC_URL=/nameContextRoot放在.env文件中(上下文根的名称通常是war文件的名称)
  3. 构建npm run build项目npm run build

对我来说,它在react项目的build目录中生成其内容

  1. 将构建目录的内容复制到spring-boot apps static目录

static目录应该是src/main/resources的普通文件夹

  1. 清理并重建spring boot项目并运行它

  2. 现在可以正确提供内容了

更新

弹簧靴结构

D:.
├───.mvn
│   └───wrapper
├───.settings
├───src
│   ├───main
│   │   ├───java
│   │   │   └───my.java.stuff
│   │   ├───resources
│   │   │   └───static
│   │   │       └───static
│   │   │           ├───css
│   │   │           └───js
│   │   └───webapp
│   └───test
│       └───java
│           └───my.java.tests
├───target
│   ├───classes
│   │   ├───java.classes
│   │   └───static
│   │       ├───css
│   │       ├───js
│   │       └───static
│   │           ├───css
│   │           └───js
│   ├───generated-sources
│   │   └───annotations
│   ├───m2e-wtp
│   │   └───web-resources
│   │       └───META-INF
│   │           └───maven
│   │               └───java.stuff
│   ├───maven-archiver
│   ├───maven-status
│   │   └───maven-compiler-plugin
│   │       └───compile
│   │           └───default-compile
│   ├───rclient-0.0.1-SNAPSHOT
│   │   ├───META-INF
│   │   └───WEB-INF
│   │       ├───classes
│   │       │   ├───java stuff
│   │       │   └───static
│   │       └───lib
│   └───test-classes
└───WebContent
    └───META-INF

ReactJS项目结构

D:.
├───build
│   └───static
│       ├───css
│       └───js
├───node_modules
├───public
└───src
    ├───components
    ├───domain
    └───util

您的api端点也将在上下文根下提供。 他们与React无关。

@Codo使用反向代理访问静态内容的提示仍然有效。 但是在这里,您特别询问了如何在spring boot应用程序中托管react应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM