【问题标题】:Trying to serve static content from my SpringBoot app, but it fails尝试从我的 SpringBoot 应用程序提供静态内容,但失败了
【发布时间】:2020-02-09 16:15:07
【问题描述】:

我想从我的 SpringBoot 应用程序中提供静态文件。我有一个非常简单的控制器,我希望它能做这些事情:

@EnableWebMvc
@RestController
public class MyRestController implements WebMvcConfigurer {

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/static/**")
               .addResourceLocations("classpath:/static")
               .addResourceLocations("file:/static");
   }

   @PostMapping(path = "/hello")
   public MyResponse hello(@RequestBody() MyBody body,
                         HttpServletRequest request) {
       return new MyResponse("Hello " + request.getRemoteAddr());
   }
}

我的 index.html 文件位于 static 文件夹中:

MyApp/
   src/
      main/
         static/index.html
         static/img/image.png

当我使用 curl 向 http://localhost:8080 发出 GET 请求时,我得到响应代码 404 作为回报,服务器状态为 No mapping for GET /。 我希望返回 index.html 文件。

不过,使用 MyBody 对象作为 json 主体向 http://localhost:8080/hello 发送 POST 请求是可行的!

我做错了什么?

我从 Spring 网站上阅读了这个 blogpost,但自从该帖子于 2013 年发布以来,它似乎已经过时了。也许它今天的工作方式有所不同?

【问题讨论】:

标签: java spring-boot spring-restcontroller


【解决方案1】:

静态资源通常位于 /src/main/resources 下以进入 maven 标准项目布局中的类路径,Spring Boot 应该为 /static (/src/main/resources/static) 下的所有文件提供服务,而不需要任何 @987654322 @应用配置。

https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

【讨论】:

    【解决方案2】:

    我认为您缺少 resources 文件夹,您的文件夹结构应该如下所示

    MyApp/
        src/
          main/
            resources/
              static/index.html
              static/img/image.png
    

    【讨论】:

      【解决方案3】:

      你不应该在 Spring Boot 中使用 EnableWebMvc。见thedocumentation

      【讨论】:

        【解决方案4】:

        这个在Spring BootDocumentation中提到,在spring mvc部分下可以使用WebMvcConfigurer,但不需要做@EnableWebMvc

        所以你应该删除@EnableWebMvc 注释!

        //@EnableWebMvc Remove this
        @RestController
        public class MyRestController implements WebMvcConfigurer {
        
           @Override
           public void addResourceHandlers(ResourceHandlerRegistry registry) {
               registry.addResourceHandler("/static/**")
                       .addResourceLocations("classpath:/static")
                       .addResourceLocations("file:/static");
           }
        
           @PostMapping(path = "/hello")
           public MyResponse hello(@RequestBody() MyBody body,
                                 HttpServletRequest request) {
               return new MyResponse("Hello " + request.getRemoteAddr());
           }
        }
        

        【讨论】:

          【解决方案5】:

          我有一个博客应用程序,可以在静态/之外的文件夹中接收上传的图像。

          MyApp/
          src/
            main/
              resources/
                    static/
                        css/
                        javascript/
                        images/
                    blog/
                        1-blogpost/ (here are the uploaded images)
                        2-blogpost/ (here are the uploaded images)
                        (... and so on)
          

          所以我用 Spring Boot 在 Kotlin 中做了这个:

          @Configuration
          class WebConfiguration : WebMvcConfigurer {
          
              private val logger = loggerFactory.getLogger(WebConfiguration::class.java)
          
              override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
                  logger.info("####### Entering ResourceHandlers configurations #######")
                  registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")
                  registry.addResourceHandler("/blog/**").addResourceLocations("file:src/main/resources/blog/")
              }
          
              @Bean
              fun restTemplate() : RestTemplate {
                  return RestTemplate()
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-24
            • 2018-09-28
            • 1970-01-01
            • 2016-05-02
            • 2010-11-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多