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


当前回答

不是说过了一年多才让死人复活,但之前所有的答案都忽略了一些关键点:

@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进行注释。

其他回答

我使用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/");
    }

}

请记住,您的代码现在负责管理所有静态资源路径。

有同样的问题,使用gradle和eclipse,并花了几个小时试图解决它。

不需要编码,诀窍是你必须使用菜单选项New->Source Folder(不是New-> Folder)来创建src/main/resources下的静态文件夹。不知道为什么这样工作,但做了new ->源文件夹然后我命名为静态文件夹(然后源文件夹对话框给出一个错误,你必须检查:更新排除过滤器在其他源文件夹解决嵌套)。我的新静态文件夹添加了index.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,在这种情况下,您将不会收到静态内容。

有两件事需要考虑(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上启动的服务器,应用程序部署到根上下文)。