React + Spring Boot 部署 [英] React + Spring Boot deployment

查看:61
本文介绍了React + Spring Boot 部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Spring Boot 部署 react nodejs 应用程序时遇到问题.spring 应用程序在基本的 HTML/JS 上运行良好,而且我的 react 应用程序在开发模式下运行良好,但我似乎缺少正确捆绑 react 和 spring 的东西.

I'm having problems deploying a react nodejs application with a spring boot. The spring application works fine with basic HTML/JS and I've got the react app running fine in dev mode, but I seem to be missing something as far as bundling the react and spring properly.

目前,我可以使用一些简单的 HTML 和 javascript ajax 调用来运行/部署我的 Spring Boot 应用程序(无反应).它在我的 AWS ec2 上运行没有问题.

Currently, I can run/deploy my spring boot application with some simple HTML and javascript ajax calls (no react). It runs on my AWS ec2 with no problems.

我还可以通过 node JSnpm start"在开发模式下运行我的 react 应用程序,并将其指向我的本地 spring boot 应用程序.(我将 "proxy": "http://localhost:8080" 添加到 manifest.json).我正在努力将我的 React 应用程序与 Spring 应用程序的其余部分实际捆绑到战争中并使其正常工作.我什至无法在没有休息调用的情况下加载一个 react hello-world 页面.

I can also run my react application in dev mode through node JS "npm start" and point it at my local spring boot application. (I added "proxy": "http://localhost:8080" to manifest.json). I'm struggling with the next steps of actually bundling my react application into the war with the rest of my spring application and getting it working. I can't even get a react hello-world page to load without rest calls.

我尝试运行 npm run build 然后将 /build 目录中生成的所有文件复制到我的 spring 项目的 /src/main/资源.具体我复制了:

I've tried running npm run build and then copying all of the files generated in the /build directory to my spring project's /src/main/resources. Specifically I copied:

react-app/build/static/* -> spring-app/src/main/resources/static/.

react-app/build/* -> spring-app/src/main/resources/.

在构建战争 (mvn install) 并在本地运行战争之后,在尝试加载服务器时未指定任何页面时出现 404 错误.当我指定 index.html 时,我收到错误,指出它无法在我的静态目录中加载 JS 和 CSS 文件.当我将 war 部署到 ec2 并尝试加载页面时,也会发生同样的问题.

After building the war (mvn install) and running the war locally, I'm getting 404 errors when trying to load the server without any page specified. When I specify index.html, I'm getting errors that it's failing to load the JS and CSS files in my static directory. The same problems happen when I deploy the war to ec2 and try to load pages.

是否有我遗漏的更多配置步骤?我知道可能需要进行一些基本 URL 配置才能在开发模式之外使用其余 API,但我什至无法在没有任何其余调用的情况下加载页面.我尝试将 index.html 移动到各种位置,例如 /resources/static/resources/public.任何帮助将不胜感激.

Are there more configuration steps that I'm missing? I'm aware there's probably some base URL config I'll need to do to consume the rest API outside of dev mode, but I can't even get a page to load without any rest calls. I tried moving index.html to various locations like /resources/static and /resources/public. Any help would be appreciated.

推荐答案

从 Spring 开始查看本教程,您可能会发现frontend-maven-plugin"非常有用.我会放弃战争,将你的 Spring Boot 应用程序构建为一个 jar,然后将你的 js 文件放在 src/main/js 中,并将你的 index.html 放在/src/main/resources/static 中.

Look into this tutorial from Spring, there is a "frontend-maven-plugin" that you might find very useful. I would ditch the war and just build your spring boot app as a jar, then place your js files in src/main/js, and your index.html in /src/main/resources/static.

教程可在此处获得:

https://spring.io/guides/tutorials/react-and-spring-data-rest/

代码可以在这里找到:

https://github.com/spring-guides/tut-react-and-spring-data-rest/tree/master/basic

使用与教程中使用的类似的 webpack.config.js 配置,一旦您使用 maven 前端插件构建,它就会正常工作".

Use a webpack.config.js config similar to the one used in the tutorial, and it will "just work" once you build using the maven front end plugin.

正如其他人所建议的,最好将前端和后端代码分开(将它们保存在单独的存储库中).

As others have suggested, you will be better off separating your front-end and back-end code (keep them in separate repositories).

这篇关于React + Spring Boot 部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆