通过 Spring Boot 在 jar 文件中提供静态资源

nsa*_*ari 5 static-resource maven bounded-contexts spring-boot

我正在通过多个 maven 模块开发一个解决方案来处理不同的有界上下文。

多亏了 spring boot,每个模块都公开了自己的 rest api,并且所有这些都托管在一个 maven 模块中,并带有一个由 @SpringBootApplication 注释的类。简化的项目结构如下所示:

parent  
|-- pom.xml  
|-- ...
|-- host   
  |-- Application.java      
  |-- resources
     |-- index.html
|-- driver
  |-- api     
  |-- resources
     |-- register.html
|-- notify
  |-- ...
|-- passenger
  |-- ...
Run Code Online (Sandbox Code Playgroud)

在面对复合 UI 时,我尝试使用相同的模式:一个保存布局、静态资源的地方,同时,html 页面属于保存在其 maven 模块中的每个有界上下文。

问题是我在 spring boot doc 中发现,无法从其他 jar 文件中提供静态资源。

是否有任何解决方案可以获得此功能,或者这里是否存在任何架构错误?或者弹簧之外的东西(例如覆盖层)是解决方案吗?

小智 5

扩展 WebMvcConfigurerAdapter 并覆盖 addResourceHandlers

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/");
}
Run Code Online (Sandbox Code Playgroud)

参考spring-boot-app-does-not-serve-static-resources-after-packaging-into-jar