通过Spring Boot在jar文件中提供静态资源 [英] Serve static resources within jar files by Spring boot

查看:202
本文介绍了通过Spring Boot在jar文件中提供静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a solution by multiple maven modules to handle different bounded contexts.

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

Thanks to spring boot each module exposes own rest api and all of them hosted in one maven module with a class that annotated by @SpringBootApplication. The simplified project structure is looks like this:

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

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

I've tried to use the same pattern when facing with the composite UI: one place that keeps layouts, static resources and in the meantime, the html pages belong to each bounded context that kept in it's maven module.

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

The problem is as I've found in the spring boot doc there is no way to serves static resources from the other jar files.

有没有解决方案来获得此功能,或者这里有体系结构上的错误?还是弹簧之外的东西(例如覆盖层)是解决方案?

Is there any solution to get this functionality or is there any architectural mistake here? or something outside of the spring (e.g. overlay) is the solution?

推荐答案

扩展了WebMvcConfigurerAdapter并覆盖了addResourceHandlers

extends WebMvcConfigurerAdapter and override addResourceHandlers

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

请参考 spring-boot-应用程序不将静态资源打包后放入罐子中

这篇关于通过Spring Boot在jar文件中提供静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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