我正在尝试使用 create-react-app
和 Spring Boot 创建一个简单的 Web 应用程序,但是 spring 在资源中找不到 index.html
。
React 的 build
文件夹被 maven-resources-plugin
复制到 target
:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
...
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
...
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/app/build</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</plugin>
这是我的项目结构:
Controller :
@Controller
public class BasicController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "index";
}
}
获取对 localhost:8080
的请求返回 404
。你能指出我错在哪里吗?
更新:
通过将 maven 插件中 React 的构建输出目录更改为
${basedir}/src/main/resources/META-INF/resources
并且 return "index"
到 return index.html
。
最佳答案
好吧,这可能不是您问题的准确答案,但我会尝试示例 from the docs .
Spring Boot will automatically add static web resources located within any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
In the case of the Consuming a RESTful Web Service with jQuery guide, we included index.html and hello.js files in the /public/ folder. This means that not only does Spring Boot offer a simple approach to building Java or Groovy apps, you can also use it to easily deploy client-side JavaScript code and test it within a real web server environment!
只需将 CRA 构建目录的内容复制到 Spring Boot public
目录(确保 index.html 在 /public/index.html
)。
如果可行,则尝试自动化该过程。
关于java - Spring Boot 和 React 无法加载 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46590228/