Spring boot 如何将Spring boot web app server配置为服务器外部内容加上使用默认资源目录?
我有一个小型的spring boot应用程序,可以下载和提供内容 某些上下文:复制和服务Spring boot 如何将Spring boot web app server配置为服务器外部内容加上使用默认资源目录?,spring-boot,spring-mvc,Spring Boot,Spring Mvc,我有一个小型的spring boot应用程序,可以下载和提供内容 某些上下文:复制和服务 @Configuration @EnableWebMvc @Slf4j /** * Exists to allow serving up static content from the filesystem */ class MvcConfig implements WebMvcConfigurer { @Autowired AppConfig appConfig @Over
@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/")
}
该应用程序在灾难恢复盒上运行。它使用spring调度器通过RESTAPI定期从我们的wiki/confluence下载一组html页面,然后通过嵌入式tomcat提供相同的.html文件
i、 e.因此,如果主数据中心或数据库/etc不可用,DR数据中心可提供“说明”
一匹有两个技巧的小马。只需要几行代码。谢谢春天
使用Spring Boot提供外部内容
我得到了如何使用自定义WebMVCConfiguer从Spring boot提供外部内容的说明[请参阅下面的代码]
但是失去了默认的“免费”行为
通过spring boot添加自定义配置程序“带走”了所有“免费获取的url映射内容”,例如,自动将“/resources”目录作为url提供给浏览器。e、 g.“resources/styles/site.css”用作“”
我确认:当我注释掉“WebMVCConfiguer”时,spring boot的默认“url映射到文件系统”行为按照文档所述的方式工作
问题
如何扩展WebMVCConfiguer以保留所有“免费”的默认spring引导文件到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/")
}
Spring MVC默认提供以下目录中的静态内容:
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
但问题中提到的WebMVCConfiguer不支持这些默认设置,这就是为什么只提供在“外部”位置找到的文件
但是,addResourceLocations
方法实际上支持字符串数组,因此可以执行以下操作:
@Configuration
class StaticResourcesConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("file:///tmp/external-resources/",
"classpath:/static/");
}
}
现在,如果您放置,例如,/tmp/html external.html
和src/main/resources/static/html internal.html
,那么(假设主机/端口是localhost:8080
)这两个请求都将得到满足:
HTTP GET: http://localhost:8080/html-external.html
HTTP GET: http://localhost:8080/html-internal.html
当然,如果你有一些控制器,它们也可以工作
更新1
根据注释,为了映射http://localhost:8080/
到一些预定义的index.html
:
src/main/resources/static/index.html
WebMVCConfiguer
,如下所示:这很有效。我会接受你的答案:一个后续问题:我如何将“/”映射到“index.html”?添加额外的“.html”资源处理程序似乎不起作用:,即registry.addResourceHandler(“.html”).addResourceLocations(“/static”)谢谢。你的意思是应该提供index.html吗?我应该检查一下…请看更新1的答案。请让我知道它是否适合你。感谢您,对于spring boot应用程序w/spring boot 2.2.1,我无法让第二个“addResourceHandler”工作,即将root映射到index.html,无论是在groovy还是java中