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

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

我试着把静态文件放在公共、资源和META-INF/资源中,但都不起作用。

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

知道为什么弹簧靴找不到这个吗?(我使用1.1.4 BTW)


当前回答

中配置的静态位置对/**的请求进行评估 resourceProperties。

在应用程序上添加以下内容。属性,可能是你唯一需要做的事情…

spring.resources.static-locations=classpath:/myresources/

这将覆盖默认的静态位置,即:

ResourceProperties.CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
        "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

您可能不想这样做,只需确保您的资源最终位于这些默认文件夹中的一个。

执行请求: 如果我将example.html存储在/public/example.html 然后我可以像这样访问它:

<host>/<context-path?if you have one>/example.html

如果我想要另一个uri,如<host>/<context-path>/magico/*用于类路径:/magicofiles/*中的文件,则需要更多的配置

@Configuration
class MyConfigClass implements WebMvcConfigurer

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/magico/**").addResourceLocations("/magicofiles/");
}

其他回答

只是为一个老问题补充另一个答案……人们已经提到@EnableWebMvc将阻止WebMvcAutoConfiguration加载,这是负责创建静态资源处理程序的代码。还有其他一些条件也会阻止WebMvcAutoConfiguration的加载。要明白这一点,最明确的方法是查看源代码:

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java#L139-L141

在我的例子中,我包括了一个库,它有一个从WebMvcConfigurationSupport扩展的类,这是一个将阻止自动配置的条件:

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

重要的是不要从WebMvcConfigurationSupport扩展。相反,从WebMvcConfigurerAdapter扩展。

更新:正确的方法做到这一点在5。实现WebMvcConfigurer

我使用Spring Boot 2.2,没有得到任何静态内容。我发现了两种适合我的方法:

选项#1 -停止使用@EnableWebMvc注释 该注释禁用了一些自动配置,包括从/src/main/resources/static等常用位置自动提供静态内容的部分。如果你真的不需要@EnableWebMvc,那么就从@Configuration类中移除它。

选项#2 -实现WebMvcConfigurer在你的@EnableWebMvc注释类和implementaddResourceHandlers() 你可以这样做:

@EnableWebMvc
@Configuration
public class SpringMVCConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/vendor/**").addResourceLocations("classpath:/static/vendor/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

}

请记住,您的代码现在负责管理所有静态资源路径。

你检查Spring Boot参考文档了吗?

默认情况下,Spring Boot将从类路径中的/static(或/public或/resources或/META-INF/resources)文件夹或ServletContext的根目录中提供静态内容。

您还可以将您的项目与Spring MVC服务Web内容指南进行比较,或者查看Spring -boot-sample- Web -ui项目的源代码。

我使用1.3.5并通过Jersey实现托管一堆rest服务。在我决定添加一些html + js文件之前,这一切都很好。 这个论坛上给出的答案对我没有任何帮助。然而,当我在pom.xml中添加以下依赖项时,src/main/resources/static中的所有内容最终都通过浏览器显示:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<dependency>

spring-web / spring-webmvc似乎是使spring引导自动配置开启的重要传递依赖项。

如上所述,文件应该在$ClassPath/static/images/name.png, (/static or /public or /resources or /META-INF/resources)。这个$ClassPath表示main/resources或main/java目录。

如果你的文件不在标准dirs中,你可以添加以下配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/lib/**"); // like this
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // ... etc.
}
...

}