我应该使用`src / main / webapp`通过Spring Boot提供静态内容吗?

时间:2015-05-06 12:54:56

标签: spring-boot jhipster static-files

documentation of Spring Boot州:

  

如果您的应用程序是,请不要使用src / main / webapp目录   打包成罐子。

但令人惊讶的是Spring Boot Sample for static web files正在使用/src/main/webapp目录。此外JHipster正在使用webapp文件夹。

所以我很困惑。 Spring Boot文档中的警告是否已过时?现在认为使用src/main/webapp为Spring Boot jar应用程序提供静态文件是一种好习惯吗?如果没有,那么在Maven设置中使用Spring Boot时推荐的做法是什么?

3 个答案:

答案 0 :(得分:1)

来自Spring Boot Reference Documentation(强调我的):

  

默认情况下,Spring Boot将在类路径中或从根目录中提供名为/static(或/public/resources/META-INF/resources)的目录中的静态内容ServletContext 。它使用Spring MVC中的ResourceHttpRequestHandler,因此您可以通过添加自己的WebMvcConfigurerAdapter并覆盖addResourceHandlers方法来修改该行为。

在我的一些(Maven)项目中,我目前正在使用src/main/resources/static,因为默认情况下它被认为是类路径的一部分,IDE(如Eclipse)倾向于这样。

答案 1 :(得分:1)

要小心,因为 Spring Boot 2.4.x 禁用了默认 servlet(这会影响 src\main\webapp 的加载以及 WAR 文件根目录中的任何其他内容。默认情况下禁用 JAR 文件打包,但有一个所有这些的配置选项 - 参见 Spring boot: configure it to find the webapp folder

https://github.com/spring-projects/spring-boot/issues/22915

完整的发行说明在这里:https://spring.io/blog/2020/11/12/spring-boot-2-4-0-available-now

答案 2 :(得分:0)

嗨,如果我理解你的问题 当你在春季靴子中使用罐子包装时 你会把你的资源放在META-INF /资源中 你可以在其他项目和调用资源中使用这个jar包, 您可以使用maven和gradle build

将webapp移动到meta-inf目录