Spring Boot 不提供静态内容 [英] Spring Boot not serving static content

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

问题描述

我无法让我的 Spring-boot 项目提供静态内容.

I can't get my Spring-boot project to serve static content.

我在 src/main/resources 下放置了一个名为 static 的文件夹.在里面我有一个名为 images 的文件夹.当我打包应用程序并运行它时,它找不到我放在该文件夹中的图像.

I've placed a folder named static under src/main/resources. Inside it I have a folder named images. When I package the app and run it, it can't find the images I have put on that folder.

我尝试将静态文件放在 publicresourcesMETA-INF/resources 中,但没有任何效果.

I've tried to put the static files in public, resources and META-INF/resources but nothing works.

如果我 jar -tvf app.jar 我可以看到文件在正确文件夹的 jar 中:/static/images/head.png 例如,但是调用:http://localhost:8080/images/head.png,我得到的只是一个404

If I jar -tvf app.jar I can see that the files are inside the jar on the right folder: /static/images/head.png for example, but calling: http://localhost:8080/images/head.png, all I get is a 404

有什么想法为什么 spring-boot 找不到这个吗?(我正在使用 1.1.4 BTW)

Any ideas why spring-boot is not finding this? (I'm using 1.1.4 BTW)

推荐答案

一年多以后不要死人复活,但是之前的所有回答都漏掉了一些关键点:

Not to raise the dead after more than a year, but all the previous answers miss some crucial points:

  1. @EnableWebMvc 在您的类上将禁用 org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.如果您想要完全控制,那很好,否则,这是一个问题.

  1. @EnableWebMvc on your class will disable org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration. That's fine if you want complete control but otherwise, it's a problem.

除了已经提供的位置之外,无需编写任何代码来为静态资源添加另一个位置.查看 v1.3.0.RELEASE 中的 org.springframework.boot.autoconfigure.web.ResourceProperties,我看到一个字段 staticLocations 可以在 应用程序中配置.属性.这是来源的一个片段:

There's no need to write any code to add another location for static resources in addition to what is already provided. Looking at org.springframework.boot.autoconfigure.web.ResourceProperties from v1.3.0.RELEASE, I see a field staticLocations that can be configured in the application.properties. Here's a snippet from the source:

/**
 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
 * /resources/, /static/, /public/] plus context:/ (the root of the servlet context).
 */
private String[] staticLocations = RESOURCE_LOCATIONS;

  • 如前所述,请求 URL 将相对解析到这些位置.因此,当请求 URL 为 /index.html 时,将提供 src/main/resources/static/index.html.从 Spring 4.1 开始,负责解析路径的类是 org.springframework.web.servlet.resource.PathResourceResolver.

  • As mentioned before, the request URL will be resolved relative to these locations. Thus src/main/resources/static/index.html will be served when the request URL is /index.html. The class that is responsible for resolving the path, as of Spring 4.1, is org.springframework.web.servlet.resource.PathResourceResolver.

    默认启用后缀模式匹配,这意味着对于请求 URL /index.html,Spring 将寻找与 /index.html.如果目的是提供静态内容,这是一个问题.要禁用它,请扩展 WebMvcConfigurerAdapter(但不要使用 @EnableWebMvc)并覆盖 configurePathMatch,如下所示:

    Suffix pattern matching is enabled by default which means for a request URL /index.html, Spring is going to look for handlers corresponding to /index.html. This is an issue if the intention is to serve static content. To disable that, extend WebMvcConfigurerAdapter (but don't use @EnableWebMvc) and override configurePathMatch as shown below:

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        super.configurePathMatch(configurer);
    
        configurer.setUseSuffixPatternMatch(false);
    }
    

  • 恕我直言,减少代码错误的唯一方法是尽可能不编写代码.使用已经提供的东西,即使需要一些研究,回报也是值得的.

    IMHO, the only way to have fewer bugs in your code is not to write code whenever possible. Use what is already provided, even if that takes some research, the return is worth it.

    修改 2021 年 7 月:

    1. WebMvcConfigurerAdapter 自 Spring 5 起已被弃用.实现 WebMvcConfigurer 并使用 @Configuration 进行注释.
    1. WebMvcConfigurerAdapter has been deprecated since Spring 5. Implement WebMvcConfigurer and annotate with @Configuration.

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

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