简体   繁体   English

Spring Boot 和 React 无法加载 index.html

[英]Spring Boot and React can't load index.html

I'm trying to create a simple web application using create-react-app and Spring Boot, but spring can't find index.html in resources.我正在尝试使用create-react-app和 Spring Boot 创建一个简单的 Web 应用程序,但是 spring 在资源中找不到index.html

React's build folder is copied to target by maven-resources-plugin : 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>

This is my project structure:这是我的项目结构:

在此处输入图片说明

在此处输入图片说明

Controller:控制器:

@Controller
public class BasicController {
   @RequestMapping(value = "/", method = RequestMethod.GET)
   public String index() {
      return "index";
   }
}

Get request to localhost:8080 returns 404 .localhost:8080获取请求返回404 Could you please point me where am i mistaken.你能指出我错在哪里吗?

UPDATE:更新:

Managed to make it working by changing React's build output directory in maven plugin to ${basedir}/src/main/resources/META-INF/resources and return "index" to return index.html .通过将 Maven 插件中 React 的构建输出目录更改为${basedir}/src/main/resources/META-INF/resourcesreturn "index"return index.html设法使其工作。

Well, it's maybe not the precise answer your question, but I would try example from the docs .好吧,这可能不是您问题的准确答案,但我会尝试文档中的示例。

Spring Boot will automatically add static web resources located within any of the following directories: Spring Boot 将自动添加位于以下任何目录中的静态 Web 资源:

/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.在使用 jQuery 指南使用 RESTful Web 服务的情况下,我们在 /public/ 文件夹中包含了 index.html 和 hello.js 文件。 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!这意味着 Spring Boot 不仅提供了一种构建 Java 或 Groovy 应用程序的简单方法,您还可以使用它轻松部署客户端 JavaScript 代码并在真实的 Web 服务器环境中对其进行测试!

Just copy the content of CRA build directory to Spring Boot public directory (make sure index.html is at the /public/index.html ).只需将 CRA 构建目录的内容复制到 Spring Boot public目录(确保 index.html 位于/public/index.html )。

If this works then try to automate the process.如果这有效,则尝试自动化该过程。

I had the same issue.我遇到过同样的问题。 Spring boot backend with reactjs frontend.带有 reactjs 前端的 Spring Boot 后端。 I solved it by adding a view resolver (thymeleaf) and copying the generated react build resources to outputDirectory/templates directory like below我通过添加视图解析器(thymeleaf)并将生成的反应构建资源复制到outputDirectory/templates目录来解决它,如下所示

<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <configuration>
                            <target>
                                <copy todir="${project.build.outputDirectory}/templates">
                                    <fileset
                                        dir="${project.basedir}/src/main/javascript/build" />
                                </copy>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM