【问题标题】:Static Files in Subfolder ClassPathResource子文件夹 ClassPathResource 中的静态文件
【发布时间】:2020-12-16 08:18:01
【问题描述】:

我要返回静态文件:

1)

resource
  |_______static
             |________assets
                        |_________css
                                   |__________bootstrap.min.css

下面是我的配置

    @Bean
    fun staticRouter(): RouterFunction<ServerResponse> {
        return RouterFunctions.resources("/**", ClassPathResource("static/**/"))
    }

当我访问localhost:8080/assets/css/bootstrap.min.css 时,我得到404 not found
我写错模式了吗?

2) 但是,如果我删除 assets,它就可以工作了

resource
  |_______static
             |________css
                        |_________bootstrap.min.css
============================================================================
    @Bean
    fun staticRouter(): RouterFunction<ServerResponse> {
        return RouterFunctions.resources("/**", ClassPathResource("static/"))
    }
============================================================================
localhost:8080/css/bootstrap.min.css, 200 OK

我添加了资产,因为这是要求。

【问题讨论】:

    标签: spring spring-boot spring-mvc spring-security spring-webflux


    【解决方案1】:

    你可以使用

    @Bean
        fun staticRouter(): RouterFunction<ServerResponse> {
            return RouterFunctions.resources("/{*filepaths}", ClassPathResource("static/**/"))
        }
    

    https://www.baeldung.com/spring-5-mvc-url-matching#1-uri-variable-syntax-foo-using-a-handler-method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2021-05-28
      • 2014-07-30
      • 1970-01-01
      相关资源
      最近更新 更多