尝试从我的 SpringBoot 应用程序提供静态内容,但失败了

Trying to serve static content from my SpringBoot app, but it fails

我想从我的 Spring 启动应用程序提供静态文件。我有一个非常简单的控制器,我希望它能完成这些工作:

@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 请求时,我在 return 中得到响应代码 404,并且服务器状态为 No mapping for GET /。 我希望 index.html 文件是 returned。

将 POST 请求发送到 http://localhost:8080/hello 并使用 MyBody 对象作为 json 正文,但是可以!

我做错了什么?

我已经从 Spring 网站上读到这篇文章 blogpost,但自从那篇 post 于 2013 年出版以来,它似乎已经过时了。也许今天它的工作方式有所不同?

静态资源通常在 /src/main/resources 下进入 maven 标准项目布局中的类路径,并且 Spring Boot 应该服务于 /static (/src/main/resources/static) 下的所有文件没有任何 addResourceHandler() 应用程序配置。

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

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

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

您不应该在 Spring Boot 中使用 EnableWebMvc。参见 the documentation

这个在Spring Boot Documentation中有提到,在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());
   }
}

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

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()
    }
}