Spring Boot不提供静态内容

编程入门 行业动态 更新时间:2024-10-16 16:48:10
本文介绍了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.

我尝试将静态文件放入public,resources和META-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,但调用: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: 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:

您班上的
  • @EnableWebMvc将禁用org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.如果您想要完全控制,那很好,但是否则,这是个问题.
  • 除了已经提供的内容外,无需编写任何代码即可为静态资源添加其他位置.从v1.3.0.RELEASE中查看org.springframework.boot.autoconfigure.web.ResourceProperties,我看到可以在application.properties中配置的字段staticLocations.这是源代码段:

  • @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.
  • 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.

    更多推荐

    Spring Boot不提供静态内容

    本文发布于:2023-11-14 04:08:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1586172.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:静态   内容   Spring   Boot

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!