【问题标题】:Running compiled Angular screens inside spring boot as well as jersey backend在 spring boot 和 jersey 后端运行编译的 Angular 屏幕
【发布时间】:2018-02-28 21:03:26
【问题描述】:

我花了很多时间努力让 spring boot 为我的角屏幕提供服务,同时也为我们的后端提供服务。我承认我对春季/球衣领域还很陌生,所以我可能没有提出正确的问题或寻找正确的东西。

类似于这篇文章Spring Boot - How To Serve Angular2 Compiled Files 我有一些编译的角度屏幕,以及一些我想在单个 jar 文件中运行的休息端点。我已经尝试过该 SO 文章中发布的示例,但没有运气。

只要不包含我的 JerseyConfig 类,我就可以让单个 index.html 文件工作

@Component
public class JerseyConfig extends ResourceConfig {

   public JerseyConfig() {
      register(WorkflowManagementResourceV1_0.class);
   }
}

但是当我删除 JerseyConfig 时,我的端点不再启用。

我也从来没有设法让编译好的 Angular 屏幕在 spring 中出现。

我尝试将东西放在资源/静态、资源/公共/webapps/resources 等中。

我很乐意添加更多内容和细节,但首先我很好奇这是否仍然可行(我假设是这样)。其次,是否需要进行某种配置、pom 依赖或自动配置才能启用此功能?

我也尝试将@Controller 添加到我的主应用程序中

@SpringBootApplication
@Controller
public class Application {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }
}

【问题讨论】:

  • 你在用ng-cli构建前端吗?
  • 我实际上正在使用一个 github 项目,所以我可以使用 maven github.com/eirslett/frontend-maven-plugin 构建它
  • 请从pom.xml添加frontend-maven-plugin构建配置
  • 来自 spring boot 项目或构建和编译我的 angular 的项目
  • 目前我正在 Wildfly 中运行这个编译好的屏幕

标签: angular spring-boot


【解决方案1】:

我遇到了类似的问题,发现以下文章非常有用:

https://www.geekmj.org/jersey/spring-boot-jersey-static-web-files-support-403/

基本上,Jersey 会接管所有请求。文章中介绍的方法是配置 Jersey 转发 404 的。由于对您的静态内容的请求是对 Jersey 的 404 请求,因此它将继续转发并由底层系统自动处理。

如果您还没有 Spring Web 依赖项,请添加它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在 application.properties 中,配置 Jersey 过滤器:

spring.jersey.type=filter

最后,将 Jersey 配置为在您的 JerseyConfig 中转发 404:

@Component
public class JerseyConfig extends ResourceConfig {

   public JerseyConfig() {
       register(WorkflowManagementResourceV1_0.class);

       // set jersey to pass 404's to the underlying system (this makes the screens work)
       property(ServletProperties.FILTER_FORWARD_ON_404, true);
   }
}

毕竟,你的静态内容应该是可用的!

【讨论】:

  • 谢谢!玩了一点点,但这正是我让它工作所需要的
  • 谢谢。我已将球衣独立 + 角度应用程序迁移到 Spring Boot。我遇到了上述问题,即球衣配置正在过滤我的屏幕,而您的修复确实有帮助。
猜你喜欢
  • 2018-07-03
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2021-10-17
  • 2018-04-14
  • 2020-09-30
  • 1970-01-01
  • 2020-07-14
相关资源
最近更新 更多