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

时间:2016-07-01 21:41:12

标签: maven spring-boot bounded-contexts static-resource

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

由于Spring引导,每个模块都会暴露自己的rest api,并且所有模块都托管在一个maven模块中,其中包含一个由@SpringBootApplication注释的类。 简化的项目结构如下所示:

parent  
|-- pom.xml  
|-- ...
|-- host   
  |-- Application.java      
  |-- resources
     |-- index.html
|-- driver
  |-- api     
  |-- resources
     |-- register.html
|-- notify
  |-- ...
|-- passenger
  |-- ...

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

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

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

1 个答案:

答案 0 :(得分:4)

扩展WebMvcConfigurerAdapter并覆盖addResourceHandlers

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

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

相关问题