使用 Spring Boot 和 Tomcat 提供前端内容 [英] Serving the frontend contents with Spring Boot and Tomcat

查看:45
本文介绍了使用 Spring Boot 和 Tomcat 提供前端内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将创建一个非常典型且不占用资源的前端+后端项目,我的一般问题是如何以更好的方式组织它.

I'm going to create a very typical and not resource-demanding frontend+backend project, and my general question is how to organize it in a better way.

后端部分提供了一个 REST API,这里将使用 Java 和 Spring Boot.关于嵌入 Tomcat 或部署 WAR 的问题仍然悬而未决.我更愿意部署 WAR,除非另一个选项对我的情况有一些优势.对于开发,我使用 Eclipse 和 Maven,最终所有这些都将在 RHEL 上运行.

The backend part provides a REST API and here Java and Spring Boot will be utilized. The question about having embedded Tomcat or deploying a WAR is still open. I would prefer to deploy a WAR, unless another option has some advantages for my case. For the development I use Eclipse and Maven, and eventually all this will work on RHEL.

前端部分是一个 Web 应用程序,它实现了一个访问 API 的 UI.这里我将使用 React.

The frontend part is a web-application implementing a UI that accesses the API. Here I'm going to use React.

很多教程建议使用frontend-maven-plugin通过Spring Boot(使用nodenpm)来提供前端内容em> 在引擎盖下).这看起来不错,紧凑.因此,我们有一个网络应用程序:

The number of tutorials suggest to utilize frontend-maven-plugin for serving the frontend content by the means of Spring Boot (using node and npm under the hood). This looks nice and compact. As a result we have a web-app at:

http://my.domain:8080/index.html

在以下位置访问 API:

accessing the API at:

http://my.domain:8080/api/...

但到目前为止我发现没有一个建议在默认的 http 端口 (80) 上运行 web 应用程序:

But no one of those I have found so far suggests to run the web-app at the default http port (80) at:

http://my.domain/index.html

我认为这是实际生产的必备条件.

which I consider as a must for real production.

也有一些方法可以将呼叫从端口 80 重定向到 8080,但这看起来是不是一个棘手的补丁?Apache Tomcat 本身可以(显然)配置为在端口 80 上运行,但这对我来说也不好看.

There are also ways to redirect calls from port 80 to 8080, but doesn't this look like a tricky patch? Apache Tomcat itself can be (apparently) configured to run on port 80, but this does not look nice for me as well.

那么,有没有一种方法可以将这样的项目组织成一个整体(最好由 Spring Boot 和 Tomcat 管理),而不会将其各个部分分散"在不同的独立服务中?

So, is there a way to organize such a project as a whole one (preferably managed by Spring Boot and Tomcat) and not having its parts "scattered" around in different independent services?

推荐答案

您可以使用 'webapp' 文件夹和此配置:

You can use 'webapp' folder and this configuration:

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("/");
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("/index.html");
    }
}

这篇关于使用 Spring Boot 和 Tomcat 提供前端内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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