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


当前回答

如上所述,文件应该在$ClassPath/static/images/name.png, (/static or /public or /resources or /META-INF/resources)。这个$ClassPath表示main/resources或main/java目录。

如果你的文件不在标准dirs中,你可以添加以下配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/lib/**"); // like this
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // ... etc.
}
...

}

其他回答

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

供参考:我还注意到,如果我添加了一个糟糕的休息控制器,我可以搞砸一个完美工作的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'。

注意,在坏控制器示例中没有映射路径。

我也遇到了类似的问题,结果证明简单的解决方案是让我的配置类扩展WebMvcAutoConfiguration:

@Configuration
@EnableWebMvc
@ComponentScan
public class ServerConfiguration extends WebMvcAutoConfiguration{
}

我不需要任何其他代码来允许我的静态内容被服务,但是,我确实把一个名为src/main/webapp下的目录和配置maven指向src/main/webapp作为资源目录。这意味着public被复制到target/classes中,因此在运行时位于spring-boot/tomcat可以找到的类路径上。

适用于Thymeleaf,可以使用链接样式表

    <link th:href="@{/css/style.css}" rel="stylesheet" />

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

这解决了我的问题