【问题标题】:how to serve static content in springboot 2.2.6?如何在 Spring Boot 2.2.6 中提供静态内容?
【发布时间】:2020-08-07 03:57:10
【问题描述】:

我是 Springboot 的新手,我正在尝试在其根 (localhost:8080) 路径上显示 html 页面。为此,我用谷歌搜索并浏览了 -

  1. Spring Boot not serving static content
  2. https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
  3. Spring Boot app not serving static content
  4. Springboot does not serve static content

几乎尝试了所有方法,但没有一个对我有用。

确切的问题

没有任何资源/(静态/或公共/或元信息/资源)中的 index.html 文件可以正常工作并显示一些弹簧数据的列表。如果我创建一个 index.html 文件,那么它会给出一个错误 404 not found without @EnableWebMvc annotation,如果使用 @EnableWebMvc 那么它会显示 Spring data rest apis 的列表。

除了 index.html 文件,它在根路径中显示 Spring 数据 api 的列表,并且 index.html 以外的 url(localhost:8080/test.html) 也有同样的问题。

使用此配置实现public class StaticResourceConfiguration implements WebMvcConfigurer 不会影响此问题。

【问题讨论】:

标签: java spring-boot static-content


【解决方案1】:

它对我有用

registry.addResourceHandler("//**").addResourceLocations("classpath:/static/");

【讨论】:

    【解决方案2】:

    simple spring boot initializer 开头

    ...我们可以将静态 (html) 文件放入以下之一:

    • (src/main/resources:)
    • 静态
    • 资源
    • 公开
    • 元信息
      • 资源

    这会产生默认(类路径)位置,通过 spring.resources.static-locations 属性配置。

    这些将通过spring.mvc.static-path-pattern-property (ref) 的值公开,默认情况下:/**

    因此,上述文件夹之一中的静态 index.html 文件(具有默认配置)可在以下位置访问:

    据此:http://localhost:8080/test.html 没问题...


    结帐at github

    所以这个,至少回答了“问题标题”“如何在springboot 2.2.6中提供静态内容?”。


    spring.resources.static-locations 的顺序(从 META-INF/resources 中首选 index.html)也是静态文件位置的“优先级”(从左到右,第一个匹配获胜)。


    当我们添加@EnableWebMvc

    ...“evertyhing 被破坏”(上下文加载,但是):

    WARN ... o.s.web.servlet.PageNotFound             : No mapping for GET /
    WARN ... o.s.web.servlet.PageNotFound             : No mapping for GET /index.html
    WARN ... o.s.web.servlet.PageNotFound             : No mapping for GET /test.html
    

    ..请考虑这个:why spring-boot application doesn't require @EnableWebMvc

    使用“非默认配置”,您必须提供更多详细信息才能找到特定的解决方案。

    但是对于“Springboot 中的新手”:从初始化程序开始,“默认值”听起来是最佳选择!从这里开始,您可以根据一个有效的配置重新优化您的配置。


    如果您出于某种原因想要/需要@EnableWebMvc 注释,这将再次导致“先前”行为/恢复 (2.2.6) 默认静态内容处理:

    @EnableWebMvc
    @SpringBootApplication
    public class DemoApplication implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
              .addResourceHandler("/**")
              .addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/");
    
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    

    (假设与现有配置/资源处理程序没有冲突)

    【讨论】:

    • 感谢您的提示,实际上 OpenApi jar 文件 github.com/springdoc/springdoc-openapi/issues/361 存在问题(我按照旧教程进行操作),实现新依赖项解决了该问题。您对此的陈述应该很有帮助,并开始消除依赖并一一检查错误。
    猜你喜欢
    • 2015-10-30
    • 2016-07-14
    • 2019-03-30
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2019-09-20
    • 2017-10-24
    相关资源
    最近更新 更多