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


当前回答

寻找映射到“/”或没有映射路径的控制器。

我就遇到过这样的问题,犯了405个错误,我的头狠狠地撞了好几天。问题出在@RestController注释的控制器上,我忘了用@RequestMapping注释。我猜这个映射路径默认为“/”,并阻止了静态内容资源映射。

其他回答

你检查Spring Boot参考文档了吗?

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

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

中配置的静态位置对/**的请求进行评估 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/");
}

如上所述,文件应该在$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.
}
...

}

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

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

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

这个解决方案对我来说很有效:

首先,在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