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


当前回答

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;
    }

这解决了我的问题

其他回答

有时候值得检查一下你是否用某个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,在这种情况下,您将不会收到静态内容。

我在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中,它就像魔术一样最终工作了。

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

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

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

配置方法如下:

@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上启动的服务器,应用程序部署到根上下文)。