【发布时间】:2020-03-16 16:41:31
【问题描述】:
我有一个小型 Spring Boot 应用程序,可以下载和提供内容。
一些背景:复制和服务
该应用程序在灾难恢复盒上运行。它使用 spring 调度器通过 rest api 定期从我们的 wiki/confluence 下载一组 html 页面,然后通过嵌入式 tomcat 提供相同的 .html 文件。
即因此,在主数据中心或数据库/等不可用的情况下,DR 数据中心中提供了“说明”。
两把小马。只需几行代码。 谢谢春天!!!
使用 Spring Boot 提供外部内容
我得到了说明如何使用自定义 WebMvcConfigurer 从 Spring Boot 提供外部内容 [参见下面的代码]
但是丢失了默认的“免费”行为
添加自定义配置器“带走”了所有“我免费获得的 url 映射东西”与 spring boot,例如自动将“/resources”目录作为 url 提供给浏览器。例如“resources/styles/site.css”充当“http://localhost/styles/site.css”
我确认:当我注释掉“WebMvcConfigurer”时,spring boot 的默认“url 映射到文件系统”行为按照记录的方式工作。
问题
我如何扩展 WebMvcConfigurer 以保留所有“免费”的默认 Spring Boot 文件到 URL 映射,但添加一个外部映射,即告诉 tomcat 服务从这个外部目录中获取内容?
谢谢!
代码
@Configuration
@EnableWebMvc
@Slf4j
/**
* Exists to allow serving up static content from the filesystem
*/
class MvcConfig implements WebMvcConfigurer {
@Autowired
AppConfig appConfig
@Override
void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/exported/**")
.addResourceLocations("file:${appConfig.outDir}/")
// I had to add this line to expose 'styles/' as a url path
registry.addResourceHandler("/styles/**")
.addResourceLocations("classpath:/public/styles/")
}
【问题讨论】: