Spring Boot和React无法加载index.html

我正在尝试使用create-react-app和Spring Boot 创建一个简单的Web应用程序,但是spring index.html在资源中找不到

反应过来的build文件夹复制到targetmaven-resources-plugin

<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
public class BasicController {
   @RequestMapping(value = "/", method = RequestMethod.GET)
   public String index() {
      return "index";
   }
}

GET请求localhost:8080返回404您能指出我在哪里吗?

更新:

通过将Maven插件中React的build输出目录更改为${basedir}/src/main/resources/META-INF/resourcesreturn "index"来使其工作return index.html

Tomasz Mularczyk:

好吧,这可能不是您问题的精确答案,但是我会尝试从docs中进行示例

Spring Boot将自动添加位于以下任何目录中的静态Web资源:

/ META-INF /资源/

/资源/

/静态的/

/上市/

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

只需将CRA构建目录的内容复制到Spring Boot public目录(确保index.html位于/public/index.html)。

如果可行,请尝试使该过程自动化。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章