简体   繁体   English

如何在 springboot 2.2.6 中提供 static 内容?

[英]how to serve static content in springboot 2.2.6?

I am newbie to Springboot and I am trying to display html page on its root( localhost:8080 ) path.我是 Springboot 的新手,我试图在其根路径( localhost:8080 )上显示 html 页面。 To do so I googled and gone through -为此,我用谷歌搜索并经历了 -

  1. Spring Boot not serving static content Spring 引导不服务 static 内容
  2. https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
  3. Spring Boot app not serving static content Spring 引导应用程序不提供 static 内容
  4. Springboot does not serve static content Springboot 不提供 static 内容

Tried almost everything but none of them worked for me.尝试了几乎所有东西,但没有一个对我有用。

Exact problem确切的问题

With out index.html file inside any of resources/(static/ or public/ or meta-inf/resources) works fine and show the list of some spring data rest.没有 index.html 文件在任何资源/(静态/或公共/或元信息/资源)内工作正常并显示一些 spring 数据 Z65E8800B5C6800AAD896F888B2A 的列表。 If I create an index.html file then it gives an error of 404 not found with out @EnableWebMvc annotation, if use @EnableWebMvc then it shows the list of Spring data rest apis.如果我创建一个 index.html 文件,那么它会给出一个错误404 not found @EnableWebMvc annotation,如果使用@EnableWebMvc然后它会显示 Spring 数据列表 Z65E8800B5C6800AAD896api882A62。

Other than index.html file it show the list of Spring data api in root path, and url( localhost:8080/test.html ) to other than index.html has same problem. Other than index.html file it show the list of Spring data api in root path, and url( localhost:8080/test.html ) to other than index.html has same problem.

This problem has no effects by implementing public class StaticResourceConfiguration implements WebMvcConfigurer with this configuration to.此问题通过实现public class StaticResourceConfiguration implements WebMvcConfigurer与此配置没有影响。

Starting with a simple spring boot initializer从一个简单的 spring 启动初始化程序开始

... we can place static (html) files into one of: ...我们可以将 static (html) 文件放入以下文件之一:

  • (src/main/resources:) (src/main/资源:)
  • static static
  • resources资源
  • public上市
  • META-INF元信息
    • resources资源

which results in the default (class path) locations, configured via the spring.resources.static-locations property.这导致默认(类路径)位置,通过spring.resources.static-locations属性配置。

These will be exposed through the value of spring.mvc.static-path-pattern -property ( ref ), by default: /** .这些将通过spring.mvc.static-path-pattern -property ( ref ) 的值公开,默认情况下: /**

So a static index.html file in one of the above mentioned folders, with default config, will be accessible at:因此,可以在上述文件夹之一中使用默认配置访问 static index.html 文件:

Accordingly: no problem with http://localhost:8080/test.html...因此: http://localhost:8080/test.html 没有问题...


Checkout at github .在 github结帐。

So this, at least answers the "question title" "how to serve static content in springboot 2.2.6?".所以这个,至少回答了“问题标题”“如何在springboot 2.2.6中提供static内容?”。


The order of spring.resources.static-locations appears (index.html preferred from META-INF/resources) also to be the "precedence" of static file locations (left-to-right, first match wins). spring.resources.static-locations的顺序出现(索引.html 从 META-INF/resources 首选)也是 ZA81259CEF8E959C624DF1D456 文件位置的“优先级”(第一个匹配项)。


When we add @EnableWebMvc当我们添加@EnableWebMvc

..."evertyhing gets broken" (context loads, but) only: ...“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

..please aslo consider this: why spring-boot application doesn't require @EnableWebMvc ..请考虑一下:为什么 spring-boot 应用程序不需要@EnableWebMvc

With "non-default config", You would have to provide more details, to find a specific solution.使用“非默认配置”,您必须提供更多详细信息才能找到特定的解决方案。

But for "newbie in Springboot": starting with an intializer and "the defaults" sounds optimal, From here on.但是对于“Springboot 中的新手”:从初始化程序开始,“默认值”听起来是最佳的,从这里开始。 you can re-fine your configuration based on a working one.您可以根据工作中的配置重新优化您的配置。


And if you want/need the @EnableWebMvc annotation for some reason, this will result in the "previous" behavior again/restore the (2.2.6) default static content handling:如果您出于某种原因想要/需要@EnableWebMvc注释,这将再次导致“以前的”行为/恢复(2.2.6)默认 static 内容处理:

@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);
    }

}

(Assuming no conflicts with existing configuration/resource handlers) (假设与现有配置/资源处理程序没有冲突)

It's working for me它对我有用

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

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

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