我无法让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)
我有这个确切的问题,然后意识到我在我的application.properties中定义了:
spring.resources.static-locations=file:/var/www/static
这压倒了我所做的一切努力。在我的情况下,我想要两者都保留,所以我只保留了财产,并添加:
spring.resources.static-locations=file:/var/www/static,classpath:static
将src/main/resources/static中的文件作为localhost:{port}/file.html。
以上这些对我来说都没用,因为没有人提到这个可以轻易从网上复制的小属性,以满足不同的目的;)
希望能有所帮助!我想它会很适合这个有这个问题的人的答案的长帖子。
我认为前面的回答很好地解决了这个问题。然而,我要补充的是,当你在应用程序中启用了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
}
.....
}
我使用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 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>