[英]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
.env
文件(在package.json之后) PUBLIC_URL=/nameContextRoot
放在.env
文件中(上下文根的名稱通常是war文件的名稱) npm run build
項目npm run build
對我來說,它在react項目的build
目錄中生成其內容
static
目錄 static
目錄應該是src/main/resources
的普通文件夾
清理並重建spring boot項目並運行它
現在可以正確提供內容了
更新
彈簧靴結構
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.