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


当前回答

我认为前面的回答很好地解决了这个问题。然而,我要补充的是,当你在应用程序中启用了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
    }
.....
}

其他回答

寻找映射到“/”或没有映射路径的控制器。

我就遇到过这样的问题,犯了405个错误,我的头狠狠地撞了好几天。问题出在@RestController注释的控制器上,我忘了用@RequestMapping注释。我猜这个映射路径默认为“/”,并阻止了静态内容资源映射。

如果从IDE中启动应用程序时出现问题(即从Eclipse或IntelliJ Idea启动),并使用Maven,解决方案的关键在Spring-boot Getting Started文档中:

如果你正在使用Maven,执行: MVN包&& Java -jar target/gs-spring-boot-0.1.0.jar

其中重要的部分是添加要在应用程序实际启动之前运行的包目标。(想法:运行菜单,编辑配置…,添加,然后选择运行Maven目标,并在字段中指定包目标)

给定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 Boot v1.5.2.RELEASE)- 1)检查所有控制器类的@EnableWebMvc注释,如果有的话删除它 2)检查使用注释的Controller类- @RestController或@Controller。不要将Rest API和MVC行为混合在一个类中。对于MVC使用@Controller,对于REST API使用@RestController

以上两件事解决了我的问题。现在我的春季引导加载静态资源没有任何问题。 @Controller => load index.html =>加载静态文件。

@Controller
public class WelcomeController {

    // inject via application.properties
    @Value("${welcome.message:Hello}")
    private String message = "Hello World";

    @RequestMapping("/")
    public String home(Map<String, Object> model) {
        model.put("message", this.message);
        return "index";
    }

}

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


    <link rel="stylesheet/less" th:href="@{/webapp/assets/theme.siberia.less}"/>

    <!-- The app's logic -->
    <script type="text/javascript" data-main="/webapp/app" th:src="@{/webapp/libs/require.js}"></script>
    <script type="text/javascript">
        require.config({
            paths: { text:"/webapp/libs/text" }
        });
    </script>



   <!-- Development only -->
     <script type="text/javascript" th:src="@{/webapp/libs/less.min.js}"></script>


</head>
<body>

</body>
</html>

配置方法如下:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcAutoConfigurationAdapter {

// specific project configuration

}

重要的是,您的WebMvcConfig可能会覆盖addResourceHandlers方法,因此您需要显式调用super.addResourceHandlers(注册表)(如果您对默认资源位置感到满意,则不需要覆盖任何方法)。

这里需要注释的另一件事是,那些默认的资源位置(/static、/public、/resources和/META-INF/resources)只有在还没有映射到/**的资源处理程序时才会被注册。

从现在开始,如果您在src/main/resources/static/images上有一个名为image.jpg的图像,例如,您可以使用以下URL访问它:http://localhost:8080/images/image.jpg(作为在端口8080上启动的服务器,应用程序部署到根上下文)。