简体   繁体   English

如何动态地将静态资源添加到spring-boot jar应用程序?

[英]How to dynamically add static resources to spring-boot jar application?

I have spring-boot application which creates catalog with html static pages. 我有一个spring-boot应用程序,它使用html静态页面创建目录。 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? 当我通过命令启动应用程序时: mvn spring-boot:run一切正常(在/resources/目录中创建了带有静态页面的文件夹,并且客户端可以访问页面),但是我希望将我的应用程序部署为jar文件,在这里我有问题,我如何实现我的目标? 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. 我知道不能将动态资源添加到jar中,但是也许可以在jar文件旁边创建包含资源的文件夹,并以某种方式将此文件夹添加到公共资源中,以便客户端可以访问html页面。

Try prefixing you resource location with file: , for example: 尝试为您的资源位置加上file:前缀,例如:

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

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 . 您也可以使用绝对路径,例如file:/path/to/resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM