gpt4 book ai didi

java - 使用静态内容部署 spring boot 应用程序的正确方法是什么

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:01 25 4
gpt4 key购买 nike

我有一个使用 gradle 构建的 Spring Boot Web 应用程序,它完全基于 JavaConfig,结构如下所示。

File Structure

根据网络上的文档和信息,我可以简单地构建一个 jar 文件,将其放入任何服务器,运行该 jar 文件,然后我的应用程序就启动并运行了。但是,当我直接运行 jar 文件时,应用程序无法找到我的 css、js 或任何图像文件。如您所见,当我在 Thymeleaf 查找我的模板时遇到问题时,我确实复制了模板文件夹。这是我的 WebConfig 类...

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

}


@Bean
MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("1024KB");
factory.setMaxRequestSize("1024KB");
return factory.createMultipartConfig();
}

}

我需要做什么才能访问正在运行的 .jar 文件中的 .css 和 .js 文件。我需要做任何额外的配置才能使这些文件在生产中可用吗?当我从 src 文件夹位置通过 gradle 运行应用程序时,它工作正常。

我很感激我能得到的任何指点。谢谢。

最佳答案

您会发现此链接非常有用。 Serving Static Web Content with Spring Boot

While this may not be a new revelation to those of you that have been following Spring Boot since the SpringOne announcement, there is one detail for which you may not be aware. Spring Boot will automatically add static web resources located within any of the following directories:

/META-INF/resources/
/resources/
/static/
/public/

关于java - 使用静态内容部署 spring boot 应用程序的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22403617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com