如何动态添加静态资源到spring-boot jar应用程序? [英] How to dynamically add static resources to spring-boot jar application?

查看:645
本文介绍了如何动态添加静态资源到spring-boot jar应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有spring-boot应用程序,它使用html静态页面创建目录。当我按命令启动应用程序时: mvn spring-boot:run 一切正常(带有静态页面的文件夹在 / resources / 目录和客户端可以访问页面)但我希望我的应用程序部署为jar文件,在这里我有我的问题,我怎样才能实现我的目标?我知道我不能动态地将资源添加到jar,但也许我可以创建包含我的jar文件旁边的资源的文件夹,并以某种方式将此文件夹添加到公共资源,以便客户端可以访问html页面。

I have spring-boot application which creates catalog with html static pages. When I start application by command: mvn spring-boot:run everything works good (folder with static pages is created in /resources/ catalog and client have access to pages) but I want have my app deployed as jar file and here I have my question, how can I achieve my goal? I understand that I can't add dynamically resources to jar but maybe I can create folder with resources next to my jar file and somehow add this folder to public resources so client could have access to html pages.

推荐答案

尝试使用文件:为资源位置添加前缀,例如:

Try prefixing you resource location with file:, for example:

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

此路径相对于启动应用程序的目录。您还可以使用绝对路径,例如 file:/ path / to / resources

This path is relative to the directory from which you launched the application. You can also use an absolute path, for example file:/path/to/resources.

这篇关于如何动态添加静态资源到spring-boot jar应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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