![](/img/trans.png)
[英]Spring Boot map URL request to a Static Resource, not serving index.html by default
[英]Default resource handler for an URL path serving a static file in Spring Boot MVC
我有一个基于 Spring Boot 的项目(基于spring-boot-starter-parent
和spring-boot-starter-web
,我想在其中提供静态内容(内置在另一个模块中,并使用maven-resources-plugin
复制到项目中) maven-resources-plugin
)。
使用默认配置/约定,它会在 misc 中查找静态内容。 位置,如classpath:/static
和classpath:/public
,如果要从根 URL 提供静态内容,这很有用,但对于本项目而言并非如此。
我想通过从WebMvcConfigurer
(并用@Configuration
注释该类)并覆盖public void addResourceHandlers(ResourceHandlerRegistry registry)
方法成功地完成/web/
服务; registry.addResourceHandler("/web/**").addResourceLocations("classpath:/static/web/");
这在显式引用/web/
文件夹中的文件名时有效,但我无法弄清楚如何模拟“默认文档”行为,例如应该为/web/
提供index.html
。 如果您将index.html
放在静态内容的根目录之一中,它将由WelcomePageHandlerMapping
处理。
找到解决办法; 我还必须覆盖addViewControllers()
以创建显式映射并修复优先级:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/web/").setViewName("index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.