使用 Spring Boot 提供动态变化的静态内容 [英] Serve dynamically changing static content with Spring Boot

查看:55
本文介绍了使用 Spring Boot 提供动态变化的静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个简单的 Spring Boot 应用程序,它提供我放置在 resources/static/img 中的静态图像.这适用于显示实际内容,但有两件事我想解决:

Right now I have a simple Spring Boot application that serves static images that I have placed in resources/static/img. This works just fine for displaying the actual content, but there are two things I'd like to fix:

  1. 我不希望这些图像中的任何一个与生成的 .jar 文件捆绑在一起,并且我知道将这些图像放在 resources 文件夹中会这样做.

  1. I don't want any of these images to be bundled with the resulting .jar file, and I know that placing these images in the resources folder will do that.

使用我当前的设置,为了在 web 应用程序上看到新图像,我必须将它添加到文件夹并重新启动它.我希望 Spring 提供存在于特定文件夹中的任何静态内容,因此我可以在应用程序运行时添加图像,并让它们在 localhost:8080/img/{image name} 自动提供.

Using my current setup, in order to see a new image on the webapp, I have to add it to the folder and restart it. I would instead like Spring to serve any static content that exists in a specific folder, so I can add images while the application is running and have them automatically served at localhost:8080/img/{image name}.

我试图通过设置一个资源处理程序来解决这个问题,但我不确定这是否与简单地从 resources/static 提供它们有什么不同.不幸的是,我仍在努力正确配置它,因为我根本看不到任何图像.这是我尝试过的:

I attempted to solve this by setting up a resource handler, but I'm not sure if this is any different than simply serving them from resources/static. Unfortunately, I'm still struggling with getting this configured correctly, as I'm unable to see any images at all. Here's what I tried:

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + Application.IMAGE_DIR);
        super.addResourceHandlers(registry);
    }
}

这是我的应用程序配置:

And here is my Application configuration:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    static String IMAGE_DIR;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws IOException {
        SpringApplication.run(Application.class, args);
        IMAGE_DIR = new File(".").getCanonicalPath() + "/img/";
    }

}

同样,我的目标是在我的项目的根目录中设置一个名为 img 的文件夹,它将存储我希望 web 应用程序在 localhost:8080/img 上提供的图像/{图像名称}.如果可能,我希望能够在应用程序运行时将内容添加到此文件夹中,并让 Spring 自动为它们提供服务,而无需重新启动它.

Again, my goal is to set up a folder in the root of my project called img which will store the images that I would like the webapp to serve on localhost:8080/img/{image name}. If possible, I'd like to be able to add content to this folder while the application is running and have Spring automatically serve them without having to restart it.

有人有什么想法吗?提前致谢.

Does anyone have any ideas? Thanks in advance.

推荐答案

你的方法的问题是你在 spring boot 应用程序运行后设置了 IMAGE_DIR 并且常量 IMAGE_DIR> 未初始化且为 null.修改如下:

the problem with your approach is that you set IMAGE_DIR after spring boot application is ran and the constant IMAGE_DIR is not initialized and is null. Change it as follows:

public static void main(String[] args) throws IOException {
        IMAGE_DIR = "/opt/img/";
        SpringApplication.run(Application.class, args);
    }

并删除所有 File(".").getCanonicalPath() 相关的东西,它会起作用.当您在所选目录中有新图像可以提供时,您的方法将适合您的需求.

and remove all File(".").getCanonicalPath() related stuff and it will work. Your approach will fit in your needs when you have new image in selected directory it can be served.

这篇关于使用 Spring Boot 提供动态变化的静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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