java - Spring Boot 和 React 无法加载 index.html

标签 java spring reactjs spring-mvc

我正在尝试使用 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>

这是我的项目结构:

enter image description here

enter image description here

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/

相关文章:

Java:使用 ArrayList 检查 CSV 文件中的重复行

java - Spring Maven 和 Tomcat 的 Hibernate 问题

reactjs - 如何从使用状态 Hook 的功能父组件传递 Prop ?

reactjs - 如何在 React.js 项目中包含 FontAwesome 图标

java - 如何防止直接 URL 访问 Android 应用程序中使用的文件?

java - 如何处理 Spring MVC 中的验证和严重错误

java - 无法在 Spring Boot 中运行任何单元测试

spring - 收件人端点与 SAML 响应不匹配

java - Spring Boot + MongoDB Id 查询

javascript - Redux getState 返回服务器上的初始状态