【问题标题】:How to configure Spring boot web app server to server external content plus use default resources directory?如何将 Spring Boot Web 应用程序服务器配置为服务器外部内容以及使用默认资源目录?
【发布时间】:2020-03-16 16:41:31
【问题描述】:

我有一个小型 Spring Boot 应用程序,可以下载和提供内容。

一些背景:复制和服务

该应用程序在灾难恢复盒上运行。它使用 spring 调度器通过 rest api 定期从我们的 wiki/confluence 下载一组 html 页面,然后通过嵌入式 tomcat 提供相同的 .html 文件。

即因此,在主数据中心或数据库/等不可用的情况下,DR 数据中心中提供了“说明”。

两把小马。只需几行代码。 谢谢春天!!!

使用 Spring Boot 提供外部内容

我得到了说明如何使用自定义 WebMvcConfigurer 从 Spring Boot 提供外部内容 [参见下面的代码]

但是丢失了默认的“免费”行为

添加自定义配置器“带走”了所有“我免费获得的 url 映射东西”与 spring boot,例如自动将“/resources”目录作为 url 提供给浏览器。例如“resources/styles/site.css”充当“http://localhost/styles/site.css

我确认:当我注释掉“WebMvcConfigurer”时,spring boot 的默认“url 映射到文件系统”行为按照记录的方式工作。

问题

我如何扩展 WebMvcConfigurer 以保留所有“免费”的默认 Spring Boot 文件到 URL 映射,但添加一个外部映射,即告诉 tomcat 服务从这个外部目录中获取内容?

谢谢!

代码

@Configuration
@EnableWebMvc
@Slf4j
/**
 * Exists to allow serving up static content from the filesystem
 */
class MvcConfig implements WebMvcConfigurer {

    @Autowired
    AppConfig appConfig

    @Override
    void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry
                .addResourceHandler("/exported/**")
                .addResourceLocations("file:${appConfig.outDir}/")

        // I had to add this line to expose 'styles/'  as a url path
        registry.addResourceHandler("/styles/**")
                .addResourceLocations("classpath:/public/styles/")



    }

【问题讨论】:

    标签: spring-boot spring-mvc


    【解决方案1】:

    Spring MVC 默认提供来自以下目录的静态内容:

    "classpath:/META-INF/resources/",
    "classpath:/resources/",
    "classpath:/static/",
    "classpath:/public/"
    

    但是 WebMvcConfigurer,如问题中所述,抑制这些默认值,这就是为什么只提供在“外部”位置找到的文件的原因。

    但是,addResourceLocations 方法实际上支持字符串数组,因此您可以执行以下操作:

    
    @Configuration
    class StaticResourcesConfiguration implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("file:///tmp/external-resources/", 
                                          "classpath:/static/");
        }
    }
    

    现在,如果您输入/tmp/html-external.htmlsrc/main/resources/static/html-internal.html,那么(假设主机/端口为localhost:8080)这两个请求都将得到处理:

    HTTP GET: http://localhost:8080/html-external.html
    HTTP GET: http://localhost:8080/html-internal.html
    

    当然,如果你有一些控制器,它们也可以工作

    更新 1

    根据评论,为了将http://localhost:8080/映射到一些预定义的index.html

    1. 添加src/main/resources/static/index.html
    2. 修改WebMvcConfigurer如下:
    
    
    @Configuration
    class StaticResourcesConfiguration implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("file:///tmp/external-resources/", 
                                          "classpath:/static/");
    
            registry.addResourceHandler("/")
                    .addResourceLocations("classpath:/static/index.html");
        }
    }
    
    

    【讨论】:

    • 这行得通。我会接受你的回答:一个后续:我如何保持映射 '/' 到 'index.html' ?添加额外的“.html”资源处理程序似乎不起作用:即 registry.addResourceHandler(".html").addResourceLocations("/static")
    • 谢谢。你的意思是localhost:8080 应该服务于 index.html 吗?我应该检查一下...
    • 请参阅更新 1 的答案。请让我知道它是否适合您。谢谢
    • fwiw,我无法让第二个“addResourceHandler”工作——即..e 将根映射到 index.html..——既不是在 groovy 中也不是在 java 中——对于带有 w/ 的 Spring Boot 应用程序Spring Boot 2.2.1
    猜你喜欢
    • 2018-10-23
    • 2019-07-25
    • 2019-02-27
    • 2017-04-13
    • 1970-01-01
    • 2020-03-23
    • 2021-01-12
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多