我无法让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-boot的状态不同,要让我的spring-boot jar提供内容:
我必须通过这个配置类添加专门注册我的src/main/resources/static内容:
@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
}
配置方法如下:
@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上启动的服务器,应用程序部署到根上下文)。
不是说过了一年多才让死人复活,但之前所有的答案都忽略了一些关键点:
@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;
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.
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);
}
恕我直言,在代码中减少错误的唯一方法就是不要尽可能地编写代码。使用已经提供的东西,即使这需要一些研究,但回报是值得的。
2021年7月编辑:
WebMvcConfigurerAdapter自Spring 5以来已弃用。实现WebMvcConfigurer并使用@Configuration进行注释。
这个解决方案对我来说很有效:
首先,在webapp/WEB-INF下放置一个资源文件夹,如下图所示
-- src
-- main
-- webapp
-- WEB-INF
-- resources
-- css
-- image
-- js
-- ...
第二,在spring配置文件中
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resource/**").addResourceLocations("WEB-INF/resources/");
}
}
然后,您可以访问您的资源内容,例如
http://localhost:8080/resource/image/yourimage.jpg
我认为前面的回答很好地解决了这个问题。然而,我要补充的是,当你在应用程序中启用了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 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>
我有这个确切的问题,然后意识到我在我的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。
以上这些对我来说都没用,因为没有人提到这个可以轻易从网上复制的小属性,以满足不同的目的;)
希望能有所帮助!我想它会很适合这个有这个问题的人的答案的长帖子。
有时候值得检查一下你是否用某个rest控制器重写了全局映射。简单的例子错误(kotlin):
@RestController("/foo")
class TrainingController {
@PostMapping
fun bazz(@RequestBody newBody: CommandDto): CommandDto = return commandDto
}
在上面的例子中,当你请求静态资源时,你会得到:
{
title: "Method Not Allowed",
status: 405,
detail: "Request method 'GET' not supported",
path: "/index.html"
}
原因可能是你想要将@PostMapping映射到/foo,但忘记了@RestController级别上的@RequestMapping注释。在这种情况下,所有请求都映射到POST,在这种情况下,您将不会收到静态内容。
只是为一个老问题补充另一个答案……人们已经提到@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应用程序,并阻止它从静态文件夹中提供内容
@RestController
public class BadController {
@RequestMapping(method= RequestMethod.POST)
public String someMethod(@RequestParam(value="date", required=false)String dateString, Model model){
return "foo";
}
}
在本例中,在将坏控制器添加到项目后,当浏览器请求静态文件夹中可用的文件时,错误响应是'405 Method Not Allowed'。
注意,在坏控制器示例中没有映射路径。
我在spring boot 2.1.3中也遇到了同样的问题,说资源没有找到404。我从application .properties中删除了下面的内容。
#spring.resources.add-mappings=true
#spring.resources.static-locations=classpath:static
#spring.mvc.static-path-pattern=/**,
删除了@enableWebMVC,并删除了任何WebMvcConfigurer覆盖
@EnableWebMvc
还要确保在配置中有@EnableAutoConfiguration。
然后把所有的静态资源放到src/main/resources/static中,它就像魔术一样最终工作了。
2.使用弹簧启动器。*,我有一个控制器映射到路由GetMapping({"/{var}", "/{var1}/{var2}", "/{var1}/{var2}/{var3}"})和boom我的应用程序停止服务资源。
我知道这样的路线是不可取的,但这完全取决于你正在构建的应用程序(在我的情况下,我别无选择,只能有这样的路线)
所以这里是我的黑客,以确保我的应用程序再次提供资源。我只是有一个映射到资源的控制器。因为spring会先匹配一个直接路由,然后再匹配任何有变量的路由,所以我决定添加一个控制器方法,映射到/ images /{name},并对其他资源重复相同的方法
@GetMapping(value = "/images/{image}", produces = {MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
public @ResponseBody
byte[] getImage(@PathVariable String image) {
ClassPathResource file = new ClassPathResource("static/images/" + image);
byte[] bytes;
try {
bytes = StreamUtils.copyToByteArray(file.getInputStream());
} catch (IOException e) {
throw new ResourceNotFoundException("file not found: " + image);
}
return bytes;
}
这解决了我的问题
中配置的静态位置对/**的请求进行评估
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/");
}
在我的例子中,一些静态文件没有提供,比如.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分。方向对了!
我使用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/");
}
}
请记住,您的代码现在负责管理所有静态资源路径。