gpt4 book ai didi

reactjs - 如何将 create-react-app 与 Spring Boot 一起使用?

转载 作者:行者123 更新时间:2023-12-03 13:25:46 28 4
gpt4 key购买 nike

我正在尝试将 ReactJS 应用程序与使用 Spring Boot 构建的 API 一起提供服务。我已经在/public/上运行了脚本 create-react-app - 如下面的结构所示​​ - 但是,当我尝试从 http://localhost:8080/public/index.html 访问它时,ReactJS 的所有相对路径似乎都被破坏了

为了正确从 ReactJS 加载资源,我还需要做什么?

enter image description here

最佳答案

我建议您创建两个项目:

  • 一个用于 Spring Boot 后端;
  • 一个用于 React 前端,使用 create-react-app 创建。

在开发模式下,您必须使用节点:“npm start”运行React开发服务器。您应该为 Spring Boot 应用程序定义一个代理:"proxy": "http://localhost:8080", 以添加到 package.json 中。该机制的文档是 here 。您可以像往常一样使用 Gradle 执行后端:./gradlew bootRun

在生产部署中,您可以使用 npm run build 生成 React 应用程序的构建。由 create-react-app 生成的静态文件可以放置在 Spring Boot 应用程序的静态目录中,例如 src/main/resources/static/ ( docs about static content使用 Spring Boot)

希望这会有所帮助!

关于reactjs - 如何将 create-react-app 与 Spring Boot 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892384/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com