我无法让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)


当前回答

在我的例子中,一些静态文件没有提供,比如.woff字体和一些图像。但是css和js工作得很好。

更新:让Spring Boot正确地服务于woff字体的一个更好的解决方案是配置这个答案中提到的资源过滤,例如(注意,你需要包括和排除):

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>static/aui/fonts/**</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
            <include>static/aui/fonts/**</include>
        </includes>
    </resource>
</resources>

-----旧的解决方案(工作,但会破坏一些字体)-----

另一个解决方案是使用setUseSuffixPatternMatch(false)禁用后缀模式匹配

@Configuration
public class StaticResourceConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // disable suffix matching to serve .woff, images, etc.
        configurer.setUseSuffixPatternMatch(false);
    }
}

致谢:@Abhiji确实给了我4分。方向对了!

其他回答

我使用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引导自动配置开启的重要传递依赖项。

有同样的问题,使用gradle和eclipse,并花了几个小时试图解决它。

不需要编码,诀窍是你必须使用菜单选项New->Source Folder(不是New-> Folder)来创建src/main/resources下的静态文件夹。不知道为什么这样工作,但做了new ->源文件夹然后我命名为静态文件夹(然后源文件夹对话框给出一个错误,你必须检查:更新排除过滤器在其他源文件夹解决嵌套)。我的新静态文件夹添加了index.html,现在它工作了。

中配置的静态位置对/**的请求进行评估 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/");
}

给定src/main/resources/static下的资源, 如果你添加了这段代码,那么src/main/resources/static中的所有静态内容将在"/"下可用:

@Configuration
public class StaticResourcesConfigurer implements WebMvcConfigurer {
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/");
    }
}

我认为前面的回答很好地解决了这个问题。然而,我要补充的是,当你在应用程序中启用了Spring Security时,你可能必须明确地告诉Spring允许对其他静态资源目录的请求,例如“/static/fonts”。

在我的情况下,我有“/static/css”,“/static/js”,“/static/images”默认允许,但/static/fonts/**被我的Spring安全实现阻塞。

下面是我如何解决这个问题的示例。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.....
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/", "/fonts/**").permitAll().
        //other security configuration rules
    }
.....
}